package com.moleq.mgpos.activities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import android.R.integer;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.moleq.adapter.BottleSetupAdapter;
import com.moleq.adapter.MultCouponAdapter;
import com.moleq.adapter.PrinterSetupAdapter;
import com.moleq.adapter.ReceiptSetupAdapter;
import com.moleq.adapter.RoundSetupAdapter;
import com.moleq.adapter.SelectionAdapter;
import com.moleq.adapter.StateListAdapter;
import com.moleq.adapter.TareSetupAdapter;
import com.moleq.base.EditableListener;
import com.moleq.base.MoleQBaseActivity;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.BottleEditDialog;
import com.moleq.dialog.InputTimeInfoDialog;
import com.moleq.dialog.MessageBox;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.PrinterEditDialog;
import com.moleq.dialog.ReceiptEditDialog;
import com.moleq.dialog.RoundEditDialog;
import com.moleq.dialog.StringSourceDialog;
import com.moleq.dialog.TareEditDialog;
import com.moleq.dialog.TimeSettingSelectionDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.models.AdapterDataSourceBuilder;
import com.moleq.models.BottleSetting;
import com.moleq.models.DvrSetting;
import com.moleq.models.GlobalInfo;
import com.moleq.models.InventorySetting;
import com.moleq.models.MultCouponSetting;
import com.moleq.models.PosSetting;
import com.moleq.models.PrinterSetting;
import com.moleq.models.Receipt;
import com.moleq.models.RoundingSetting;
import com.moleq.models.States;
import com.moleq.models.StoreSetting;
import com.moleq.models.TareSetting;
import com.moleq.models.TaxSetting;
import com.moleq.service.BottleSettingService;
import com.moleq.service.DvrSettingService;
import com.moleq.service.InventorySettingService;
import com.moleq.service.MultCouponSettingService;
import com.moleq.service.PosSettingService;
import com.moleq.service.PrinterSettingService;
import com.moleq.service.ReceiptService;
import com.moleq.service.RoundingSettingService;
import com.moleq.service.ServiceManager;
import com.moleq.service.StatesService;
import com.moleq.service.StoreService;
import com.moleq.service.TareSettingService;
import com.moleq.service.TaxSettingService;
import com.moleq.util.ChangeMode;
import com.moleq.util.MoleQUtil;

public class PosSettingActivity extends MoleQBaseActivity implements OnResultListener{

	public static final int HEADER_RETURN = 801;
	public static final int TRAILER_RETURN = 802;
	
	public static final int DIALOG_PRINTER = 701;
	public static final int DIALOG_ROUNDING = 702;
	public static final int DIALOG_FS_TAX_RULE = 703;
	public static final int DIALOG_CURRENCY_SIGN = 704;
	public static final int DIALOG_WEIGHT_UNIT = 705;
	public static final int DIALOG_PRICE_DIGHTS = 706;
	public static final int DIALOG_RECEIPT_PNT = 707;
	
	private StateListAdapter stateAdapter = null;
	private boolean result = false;
	private int msgType = 0;
	private boolean is_copy = false;
	
	public static final int POS_SETTING = 0;
	public static final int COMPANYINFO = 1;
	public static final int TAX_SETTING = 2;
	public static final int TARE_SETTING = 3;
	public static final int BOTTLE_SETTING = 4;
	public static final int HEADER_SETTING = 5;
	public static final int TRAILER_SETTING = 6;
	public static final int INVENTORY_SETTING = 7;
	public static final int PRINTER_SETTING = 8;
	public static final int ROUND_SETTING = 9;
	public static final int DVR_SETTING = 10;
	public static final int MULT_COUPON = 11;
	
	private AbsoluteLayout al_posSetting, al_company_info, al_tax, al_inventorySetting,al_dvrSetting,al_mult_coupon_detailed;
	
	private LinearLayout ll_pos_possetting, ll_pos_companyinfo, ll_pos_tax, 
		ll_pos_tare, ll_pos_bottle, ll_pos_header, ll_pos_trailer, 
		ll_pos_inventory,ll_pos_printer,ll_pos_rounding,ll_pos_dvr,ll_pos_mult_coupon;
	private ListView lv_choose;
	private SelectionAdapter chooseAdapter = null;

	private PosSettingService psService = null;
	private List<PosSetting> listPos = null;
	private Button btn_unit, btn_digits,btn_sign,btn_pos_save,btn_pos_undo,
			btn_pos_exit, btn_fs_tax_rule,btn_label_printer,btn_rounding,btn_receipt_printer;
	private TextView tv_id;
	private EditText et_store, et_station, et_openTime, et_closeTime, et_fcMix,
			et_tareRate,et_sig_min;
	private CheckBox cb_paper,cb_weight,cb_calendar,cb_norm,cb_networkOnoff,
			cb_showDesc,cb_zero_baln,cb_zero_price,cb_label_printer,cb_rounding,
			cb_printService,cb_sig_host,cb_save_sig;
	
	private InventorySettingService mInventorySettingService = null;
	private InventorySetting mInventorySetting = null;
	private Button btn_inv_save, btn_inv_undo, btn_inv_exit;
	private CheckBox cb_inv_server_on_off;
	private EditText et_inv_server_ip;
	
	private MultCouponSettingService mMultCouponSettingService = null;
	private List<MultCouponSetting> listMultCouponSetting;
	private MultCouponAdapter mMultCouponAdapter;
	private EditText et_mult_coupon_name,et_mult_coupon_qty,et_mult_coupon_add_value,
	et_mult_coupon_max_value,et_mult_coupon_max_total;
	private Button btn_mult_coupon_add,btn_mult_coupon_save,btn_mult_coupon_undo,btn_mult_coupon_exit,btn_mult_coupon_delete;
	private CheckBox cb_mult_coupon_active;
	private ListView lv_mult_coupon_list;
	public static final int Update_action = 0;
	public static final int Insert_action = 1;
	public static int currentProcessMode = 0;
	
	private DvrSettingService mDvrSettingService = null;
	private DvrSetting mDvrSetting = null;
	private Button btn_dvr_save, btn_dvr_undo, btn_dvr_exit;
	private CheckBox cb_dvr_server_on_off;
	private EditText et_dvr_ip,et_dvr_port;
	
	private PrinterSettingService mPrinterSettingService = null;
	private PrinterSetupAdapter printerAdapter;
	private List<PrinterSetting> listPrinter;
	private ListView lv_printerlist;
	private Button btn_pr_add, btn_pr_delete, btn_pr_edit, btn_pr_save, btn_pr_undo, btn_pr_exit;
	
	private RoundingSettingService mRoundingSettingService = null;
	private RoundSetupAdapter roundAdapter;
	private List<RoundingSetting> listRound = null;
	private ListView lv_roundlist;
	private Button btn_ro_add, btn_ro_delete, btn_ro_edit, btn_ro_save, btn_ro_undo, btn_ro_exit;
	
	private EditText et_rate, et_startvalue, et_round;
	private TaxSettingService taxService = null;
	private List<TaxSetting> listTax = null;
	private CheckBox cb_byline, cb_vat;
	private Button btn_tax_add, btn_tax_save, btn_tax_undo, btn_tax_exit;
	
	private StoreService storeService = null;
	private StatesService statesService = null;
	private List<StoreSetting> listStore = null;
	private List<States> listStates = null;
	private EditText et_name, et_desc, et_address_1, et_address_2, et_city,et_businessName,
			et_zip, et_telephone, et_fax, et_website, et_states_dialog_search;
	private Button btn_state, btn_cpy_save, btn_cpy_undo, btn_cpy_exit;
	private ListView lv_states_dialog_list;
	private Button btn_states_dialog_ok, btn_states_dialog_cancel,btn_states_dialog_search;
	
	private ListView lv_tarelist;
	private TareSetupAdapter tareAdapter;
	private TareSettingService tareService = null;
	private List<TareSetting> listTare = new ArrayList<TareSetting>();
	private Button btn_ta_add, btn_ta_delete, btn_ta_edit, btn_ta_save, btn_ta_undo, btn_ta_exit;
	
	private ListView lv_bottlelist;
	private BottleSetupAdapter bottleAdapter;
	private BottleSettingService bottleService = null;
	private List<BottleSetting> listBottle = new ArrayList<BottleSetting>();
	private Button btn_bo_add, btn_bo_delete, btn_bo_edit, btn_bo_save, btn_bo_undo, btn_bo_exit;
	
	private ImageView iv_h_copy;
	private ListView lv_header, lv_trailer;
	private ReceiptSetupAdapter headerAdapter;
	private ReceiptSetupAdapter trailerAdapter;
	private ReceiptService receiptService = null;
	private List<Receipt> listHeader, listTrailer = new ArrayList<Receipt>();
	private Button btn_header_add, btn_header_delete, btn_header_edit, btn_header_save, btn_header_undo, btn_header_exit;
	private Button btn_trailer_add, btn_trailer_delete, btn_trailer_edit, btn_trailer_save, btn_trailer_undo, btn_trailer_exit;
	
	private ListView lv_tax_list;
	private SelectionAdapter taxAdapter = null;
	private List<String> unitSource = new ArrayList<String>();
	private List<String> digitSource = new ArrayList<String>();
	private List<String> signSource = new ArrayList<String>();
	private List<String> ruleSource = new ArrayList<String>();

	private MessageDialog mMessageDialog;
	private PosSetting posSettingx;
	
	private TimeSettingSelectionDialog tdssDialog;
	private InputTimeInfoDialog itidDialog;
	
	Date now = new Date();
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_pos_setting);
		
		ArrayList<String> selectionItems = new ArrayList<String>();
		selectionItems.add("POS Setting");
		selectionItems.add("Store Info");
		selectionItems.add("Taxes");
		selectionItems.add("Tares");
		selectionItems.add("Bottles");
		selectionItems.add("Receipt Header");
		selectionItems.add("Receipt Trailer");
		selectionItems.add("Inventory");
		selectionItems.add("Printers");
		selectionItems.add("Rounding");
		selectionItems.add("DVR");
		selectionItems.add("Mult. Coupon");
		chooseAdapter = new SelectionAdapter(this, selectionItems);
		chooseAdapter.setItemTextSize(16);	
		
		al_posSetting = (AbsoluteLayout) findViewById(R.id.al_posSetting);
		al_company_info = (AbsoluteLayout) findViewById(R.id.al_company_info);
		al_tax = (AbsoluteLayout) findViewById(R.id.al_tax);
		al_inventorySetting = (AbsoluteLayout) findViewById(R.id.al_inventorySetting);
		al_dvrSetting = (AbsoluteLayout) findViewById(R.id.al_dvrSetting);
		al_mult_coupon_detailed = (AbsoluteLayout) findViewById(R.id.al_mult_coupon_detailed);
		al_posSetting.setOnClickListener(onClickListener);
		al_company_info.setOnClickListener(onClickListener);
		al_tax.setOnClickListener(onClickListener);
		al_inventorySetting.setOnClickListener(onClickListener);
		al_dvrSetting.setOnClickListener(onClickListener);
		al_mult_coupon_detailed.setOnClickListener(onClickListener);
		
		
		ll_pos_possetting = (LinearLayout) findViewById(R.id.ll_pos_possetting);
		ll_pos_companyinfo = (LinearLayout) findViewById(R.id.ll_pos_companyinfo);
		ll_pos_tax = (LinearLayout) findViewById(R.id.ll_pos_tax); 
		ll_pos_tare = (LinearLayout) findViewById(R.id.ll_pos_tare);
		ll_pos_bottle = (LinearLayout) findViewById(R.id.ll_pos_bottle);
		ll_pos_header = (LinearLayout) findViewById(R.id.ll_pos_header);
		ll_pos_trailer = (LinearLayout) findViewById(R.id.ll_pos_trailer);
		ll_pos_inventory = (LinearLayout) findViewById(R.id.ll_pos_inventory);
		ll_pos_printer = (LinearLayout) findViewById(R.id.ll_pos_printer);
		ll_pos_rounding = (LinearLayout) findViewById(R.id.ll_pos_rounding);
		ll_pos_dvr = (LinearLayout) findViewById(R.id.ll_pos_dvr);
		ll_pos_mult_coupon = (LinearLayout) findViewById(R.id.ll_pos_mult_coupon);
		lv_choose = (ListView) findViewById(R.id.lv_choose);
		lv_choose.setAdapter(chooseAdapter);
		lv_choose.setOnItemClickListener(onItemClickListener);

		tv_id = (TextView) findViewById(R.id.tv_id);
		et_store = (EditText) findViewById(R.id.et_store);
		et_station = (EditText) findViewById(R.id.et_station);
		et_openTime = (EditText) findViewById(R.id.et_openTime);
		et_closeTime = (EditText) findViewById(R.id.et_closeTime);
		et_tareRate = (EditText) findViewById(R.id.et_tareRate);
		et_fcMix = (EditText) findViewById(R.id.et_fcMix);
		btn_unit = (Button) findViewById(R.id.btn_unit);
		btn_digits = (Button) findViewById(R.id.btn_digits);
		btn_sign = (Button) findViewById(R.id.btn_sign);
		cb_paper = (CheckBox) findViewById(R.id.cb_paper);
		cb_weight = (CheckBox) findViewById(R.id.cb_weight);
		cb_calendar = (CheckBox) findViewById(R.id.cb_calendar);
		cb_norm = (CheckBox) findViewById(R.id.cb_norm); 
		cb_networkOnoff = (CheckBox) findViewById(R.id.cb_networkOnoff);
		cb_showDesc = (CheckBox) findViewById(R.id.cb_showDesc);
//		cb_dvr = (CheckBox) findViewById(R.id.cb_dvr);
		cb_zero_baln = (CheckBox) findViewById(R.id.cb_zero_baln);
		cb_zero_price = (CheckBox) findViewById(R.id.cb_zero_price);
		btn_fs_tax_rule = (Button) findViewById(R.id.btn_fs_tax_rule);
		btn_label_printer = (Button) findViewById(R.id.btn_label_printer);
		btn_rounding = (Button) findViewById(R.id.btn_rounding);
		cb_label_printer = (CheckBox) findViewById(R.id.cb_label_printer);
		cb_rounding = (CheckBox) findViewById(R.id.cb_rounding);
		cb_printService = (CheckBox) findViewById(R.id.cb_printService);
		cb_sig_host = (CheckBox) findViewById(R.id.cb_sig_host);
		cb_save_sig = (CheckBox) findViewById(R.id.cb_save_sig);
		et_sig_min = (EditText) findViewById(R.id.et_sig_min);
		btn_receipt_printer = (Button) findViewById(R.id.btn_receipt_printer);
		
		et_store.addTextChangedListener(watcher);
		et_station.addTextChangedListener(watcher);
		et_openTime.addTextChangedListener(watcher);
		et_closeTime.addTextChangedListener(watcher);
		et_fcMix.addTextChangedListener(watcher);
		et_tareRate.addTextChangedListener(watcher);
		btn_unit.addTextChangedListener(watcher);
		btn_digits.addTextChangedListener(watcher);
		btn_sign.addTextChangedListener(watcher);
		btn_fs_tax_rule.addTextChangedListener(watcher);
		btn_label_printer.addTextChangedListener(watcher);
		btn_rounding.addTextChangedListener(watcher);
		cb_paper.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_weight.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_calendar.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_norm.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_networkOnoff.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_showDesc.setOnCheckedChangeListener(onCheckedChangeListener);
//		cb_dvr.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_zero_baln.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_zero_price.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_label_printer.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_rounding.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_printService.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_sig_host.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_save_sig.setOnCheckedChangeListener(onCheckedChangeListener);
		et_sig_min.addTextChangedListener(watcher);
		btn_receipt_printer.addTextChangedListener(watcher);
		
		btn_pos_save = (Button) findViewById(R.id.btn_pos_save);
		btn_pos_undo = (Button) findViewById(R.id.btn_pos_undo);
		btn_pos_exit = (Button) findViewById(R.id.btn_pos_exit);
		btn_pos_save.setOnClickListener(onImageClickListener);
		btn_pos_undo.setOnClickListener(onImageClickListener);
		btn_pos_exit.setOnClickListener(onImageClickListener);
		btn_digits.setOnClickListener(onClickListener);
		btn_unit.setOnClickListener(onClickListener);
		btn_sign.setOnClickListener(onClickListener);
		btn_fs_tax_rule.setOnClickListener(onClickListener);
		btn_label_printer.setOnClickListener(onClickListener);
		btn_rounding.setOnClickListener(onClickListener);
		et_openTime.setOnClickListener(onTextClickListener);
		et_closeTime.setOnClickListener(onTextClickListener);
		btn_receipt_printer.setOnClickListener(onClickListener);
		
		btn_ta_add = (Button) findViewById(R.id.btn_ta_add);
		btn_ta_delete = (Button) findViewById(R.id.btn_ta_delete);
		btn_ta_edit = (Button) findViewById(R.id.btn_ta_edit);
		btn_ta_save = (Button) findViewById(R.id.btn_ta_save);
		btn_ta_undo = (Button) findViewById(R.id.btn_ta_undo);
		btn_ta_exit = (Button) findViewById(R.id.btn_ta_exit);
		btn_ta_add.setOnClickListener(onClickListener);
		btn_ta_delete.setOnClickListener(onClickListener);
		btn_ta_edit.setOnClickListener(onClickListener);
		btn_ta_save.setOnClickListener(onClickListener);
		btn_ta_undo.setOnClickListener(onClickListener);
		btn_ta_exit.setOnClickListener(onClickListener);
		lv_tarelist = (ListView) findViewById(R.id.lv_tarelist);
		lv_tarelist.setOnItemClickListener(onItemClickListener);
		
		btn_bo_add = (Button) findViewById(R.id.btn_bo_add);
		btn_bo_delete = (Button) findViewById(R.id.btn_bo_delete);
		btn_bo_edit = (Button) findViewById(R.id.btn_bo_edit);
		btn_bo_save = (Button) findViewById(R.id.btn_bo_save);
		btn_bo_undo = (Button) findViewById(R.id.btn_bo_undo);
		btn_bo_exit = (Button) findViewById(R.id.btn_bo_exit);
		btn_bo_add.setOnClickListener(onClickListener);
		btn_bo_delete.setOnClickListener(onClickListener);
		btn_bo_edit.setOnClickListener(onClickListener);
		btn_bo_save.setOnClickListener(onClickListener);
		btn_bo_undo.setOnClickListener(onClickListener);
		btn_bo_exit.setOnClickListener(onClickListener);
		lv_bottlelist = (ListView) findViewById(R.id.lv_bottlelist);
		lv_bottlelist.setOnItemClickListener(onItemClickListener);
		
		iv_h_copy = (ImageView) findViewById(R.id.iv_h_copy);
		iv_h_copy.setOnClickListener(onImageClickListener);
		btn_header_add = (Button) findViewById(R.id.btn_header_add);
		btn_header_delete = (Button) findViewById(R.id.btn_header_delete);
		btn_header_edit = (Button) findViewById(R.id.btn_header_edit);
		btn_header_save = (Button) findViewById(R.id.btn_header_save);
		btn_header_undo = (Button) findViewById(R.id.btn_header_undo);
		btn_header_exit = (Button) findViewById(R.id.btn_header_exit);
		btn_header_add.setOnClickListener(onClickListener);
		btn_header_delete.setOnClickListener(onClickListener);
		btn_header_edit.setOnClickListener(onClickListener);
		btn_header_save.setOnClickListener(onClickListener);
		btn_header_undo.setOnClickListener(onClickListener);
		btn_header_exit.setOnClickListener(onClickListener);
		lv_header = (ListView) findViewById(R.id.lv_header);
		lv_header.setOnItemClickListener(onItemClickListener);
		
		btn_trailer_add = (Button) findViewById(R.id.btn_trailer_add);
		btn_trailer_delete = (Button) findViewById(R.id.btn_trailer_delete);
		btn_trailer_edit = (Button) findViewById(R.id.btn_trailer_edit);
		btn_trailer_save = (Button) findViewById(R.id.btn_trailer_save);
		btn_trailer_undo = (Button) findViewById(R.id.btn_trailer_undo);
		btn_trailer_exit = (Button) findViewById(R.id.btn_trailer_exit);
		btn_trailer_add.setOnClickListener(onClickListener);
		btn_trailer_delete.setOnClickListener(onClickListener);
		btn_trailer_edit.setOnClickListener(onClickListener);
		btn_trailer_save.setOnClickListener(onClickListener);
		btn_trailer_undo.setOnClickListener(onClickListener);
		btn_trailer_exit.setOnClickListener(onClickListener);
		lv_trailer = (ListView) findViewById(R.id.lv_trailer);
		lv_trailer.setOnItemClickListener(onItemClickListener);
		
		btn_pr_add = (Button) findViewById(R.id.btn_pr_add);
		btn_pr_delete = (Button) findViewById(R.id.btn_pr_delete);
		btn_pr_edit = (Button) findViewById(R.id.btn_pr_edit);
		btn_pr_save = (Button) findViewById(R.id.btn_pr_save);
		btn_pr_undo = (Button) findViewById(R.id.btn_pr_undo);
		btn_pr_exit = (Button) findViewById(R.id.btn_pr_exit);
		btn_pr_add.setOnClickListener(onClickListener);
		btn_pr_delete.setOnClickListener(onClickListener);
		btn_pr_edit.setOnClickListener(onClickListener);
		btn_pr_save.setOnClickListener(onClickListener);
		btn_pr_undo.setOnClickListener(onClickListener);
		btn_pr_exit.setOnClickListener(onClickListener);
		lv_printerlist = (ListView) findViewById(R.id.lv_printerlist);
		lv_printerlist.setOnItemClickListener(onItemClickListener);
		
		btn_ro_add = (Button) findViewById(R.id.btn_ro_add);
		btn_ro_delete = (Button) findViewById(R.id.btn_ro_delete);
		btn_ro_edit = (Button) findViewById(R.id.btn_ro_edit);
		btn_ro_save = (Button) findViewById(R.id.btn_ro_save);
		btn_ro_undo = (Button) findViewById(R.id.btn_ro_undo);
		btn_ro_exit = (Button) findViewById(R.id.btn_ro_exit);
		btn_ro_add.setOnClickListener(onClickListener);
		btn_ro_delete.setOnClickListener(onClickListener);
		btn_ro_edit.setOnClickListener(onClickListener);
		btn_ro_save.setOnClickListener(onClickListener);
		btn_ro_undo.setOnClickListener(onClickListener);
		btn_ro_exit.setOnClickListener(onClickListener);
		lv_roundlist = (ListView) findViewById(R.id.lv_roundlist);
		lv_roundlist.setOnItemClickListener(onItemClickListener);
		
		et_rate = (EditText) findViewById(R.id.et_rate);
		et_startvalue = (EditText) findViewById(R.id.et_startvalue);
		et_round = (EditText) findViewById(R.id.et_round);
		cb_byline = (CheckBox) findViewById(R.id.cb_byline);
		cb_vat = (CheckBox) findViewById(R.id.cb_vat);
		et_rate.addTextChangedListener(watcher);
		et_startvalue.addTextChangedListener(watcher);
		et_round.addTextChangedListener(watcher);
		cb_byline.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_vat.setOnCheckedChangeListener(onCheckedChangeListener);
		lv_tax_list = (ListView) findViewById(R.id.lv_taxlist);
		lv_tax_list.setOnItemClickListener(onItemClickListener);
		btn_tax_add = (Button) findViewById(R.id.btn_tax_add);
		btn_tax_save = (Button) findViewById(R.id.btn_tax_save);
		btn_tax_undo = (Button) findViewById(R.id.btn_tax_undo);
		btn_tax_exit = (Button) findViewById(R.id.btn_tax_exit);
		btn_tax_add.setOnClickListener(onImageClickListener);
		btn_tax_save.setOnClickListener(onImageClickListener);
		btn_tax_undo.setOnClickListener(onImageClickListener);
		btn_tax_exit.setOnClickListener(onImageClickListener);
		
		et_businessName = (EditText) findViewById(R.id.et_businessName);
		et_name = (EditText) findViewById(R.id.et_name);
		et_desc = (EditText) findViewById(R.id.et_desc);
		et_address_1 = (EditText) findViewById(R.id.et_address_1);
		et_address_2 = (EditText) findViewById(R.id.et_address_2);
		et_city = (EditText) findViewById(R.id.et_city);
		et_zip = (EditText) findViewById(R.id.et_zip);
		et_telephone = (EditText) findViewById(R.id.et_telephone);
		et_fax = (EditText) findViewById(R.id.et_fax);
		et_website = (EditText) findViewById(R.id.et_website);
		btn_state = (Button) findViewById(R.id.btn_state);
		btn_cpy_save = (Button) findViewById(R.id.btn_cpy_save);
		btn_cpy_undo = (Button) findViewById(R.id.btn_cpy_undo);
		btn_cpy_exit = (Button) findViewById(R.id.btn_cpy_exit);
		
		et_inv_server_ip = (EditText) findViewById(R.id.et_inv_server_ip);
		cb_inv_server_on_off = (CheckBox) findViewById(R.id.cb_inv_server_on_off);
		btn_inv_save = (Button) findViewById(R.id.btn_inv_save);
		btn_inv_undo = (Button) findViewById(R.id.btn_inv_undo);
		btn_inv_exit = (Button) findViewById(R.id.btn_inv_exit);
		
		et_dvr_ip = (EditText) findViewById(R.id.et_dvr_ip);
		et_dvr_port = (EditText) findViewById(R.id.et_dvr_port);
		cb_dvr_server_on_off = (CheckBox) findViewById(R.id.cb_dvr_server_on_off);
		btn_dvr_save = (Button) findViewById(R.id.btn_dvr_save);
		btn_dvr_undo = (Button) findViewById(R.id.btn_dvr_undo);
		btn_dvr_exit = (Button) findViewById(R.id.btn_dvr_exit);
		
//		private MultCouponSettingService mMultCouponSettingService = null;
//		private List<MultCouponSetting> listMultCouponSetting;
//		private EditText et_mult_coupon_name,et_mult_coupon_qty,et_mult_coupon_add_value,
//		et_mult_coupon_max_value,et_mult_coupon_max_total;
//		private Button btn_mult_coupon_add,btn_mult_coupon_save,btn_mult_coupon_undo,btn_mult_coupon_exit;
//		private CheckBox cb_mult_coupon_active;
		et_mult_coupon_name = (EditText) findViewById(R.id.et_mult_coupon_name);
		et_mult_coupon_qty = (EditText) findViewById(R.id.et_mult_coupon_qty);
		et_mult_coupon_add_value = (EditText) findViewById(R.id.et_mult_coupon_add_value);
		et_mult_coupon_max_value = (EditText) findViewById(R.id.et_mult_coupon_max_value);
		et_mult_coupon_max_total = (EditText) findViewById(R.id.et_mult_coupon_max_total);
		btn_mult_coupon_add = (Button) findViewById(R.id.btn_mult_coupon_add);
		btn_mult_coupon_save = (Button) findViewById(R.id.btn_mult_coupon_save);
		btn_mult_coupon_undo = (Button) findViewById(R.id.btn_mult_coupon_undo);
		btn_mult_coupon_exit = (Button) findViewById(R.id.btn_mult_coupon_exit);
		btn_mult_coupon_delete = (Button) findViewById(R.id.btn_mult_coupon_delete);
		cb_mult_coupon_active = (CheckBox) findViewById(R.id.cb_mult_coupon_active);
		lv_mult_coupon_list = (ListView) findViewById(R.id.lv_mult_coupon_list);
		
		btn_mult_coupon_add.setOnClickListener(onClickListener);
		btn_mult_coupon_save.setOnClickListener(onClickListener);
		btn_mult_coupon_undo.setOnClickListener(onClickListener);
		btn_mult_coupon_delete.setOnClickListener(onClickListener);
		lv_mult_coupon_list.setOnItemClickListener(onItemClickListener);
		cb_mult_coupon_active.setOnCheckedChangeListener(onCheckedChangeListener);
		et_mult_coupon_name.addTextChangedListener(watcher);
		et_mult_coupon_qty.addTextChangedListener(watcher);
		et_mult_coupon_add_value.addTextChangedListener(watcher);
		et_mult_coupon_max_value.addTextChangedListener(watcher);
		et_mult_coupon_max_total.addTextChangedListener(watcher);
		
		ChangeMode.mode = 0;

		et_businessName.addTextChangedListener(watcher);
		et_name.addTextChangedListener(watcher);
		et_desc.addTextChangedListener(watcher);
		et_address_1.addTextChangedListener(watcher);
		et_address_2.addTextChangedListener(watcher);
		et_city.addTextChangedListener(watcher);
		et_zip.addTextChangedListener(watcher);
		et_telephone.addTextChangedListener(watcher);
		et_fax.addTextChangedListener(watcher);
		et_website.addTextChangedListener(watcher);
		btn_state.addTextChangedListener(watcher);
		btn_state.setOnClickListener(onClickListener);
		
		btn_cpy_save.setOnClickListener(onClickListener);
		btn_cpy_undo.setOnClickListener(onClickListener);
		btn_cpy_exit.setOnClickListener(onClickListener);
		
		et_inv_server_ip.addTextChangedListener(watcher);
		cb_inv_server_on_off.setOnCheckedChangeListener(onCheckedChangeListener);
		btn_inv_save.setOnClickListener(onClickListener);
		btn_inv_undo.setOnClickListener(onClickListener);
		btn_inv_exit.setOnClickListener(onClickListener);
		
		et_dvr_ip.addTextChangedListener(watcher);
		et_dvr_port.addTextChangedListener(watcher);
		cb_dvr_server_on_off.setOnCheckedChangeListener(onCheckedChangeListener);
		btn_dvr_save.setOnClickListener(onClickListener);
		btn_dvr_undo.setOnClickListener(onClickListener);
		btn_dvr_exit.setOnClickListener(onClickListener);
		
		btn_mult_coupon_exit.setOnClickListener(onClickListener);

		lv_tarelist.setDividerHeight(0);	//分割线
		lv_bottlelist.setDividerHeight(0);
		lv_header.setDividerHeight(0);
		lv_trailer.setDividerHeight(0);
		lv_printerlist.setDividerHeight(0);
		lv_roundlist.setDividerHeight(0);

		psService = ServiceManager.getInstance().mPosSettingService;
		receiptService = ServiceManager.getInstance().mReceiptService;
		taxService = ServiceManager.getInstance().mTaxSettingService;
		tareService = ServiceManager.getInstance().mTareSettingService;
		bottleService = ServiceManager.getInstance().mBottleSettingService;
		storeService = ServiceManager.getInstance().mStoreService;
		statesService = ServiceManager.getInstance().mStatesService;
		mInventorySettingService = ServiceManager.getInstance().mInventorySettingService;
		mPrinterSettingService = ServiceManager.getInstance().mPrinterSettingService;
		mRoundingSettingService = ServiceManager.getInstance().mRoundingSettingService;
		mDvrSettingService = ServiceManager.getInstance().mDvrSettingService;
		mMultCouponSettingService = ServiceManager.getInstance().mMultCouponSettingService;
		
		unitSource.add("g");
		unitSource.add("kg");
		unitSource.add("lb");
		unitSource.add("oz");

		digitSource.add("4");
		digitSource.add("5");
		
		signSource.add("-N/A-");
		signSource.add("$ - US Dollar");
		signSource.add("€ - Euro");
		
		ruleSource.add("Illinois");
		ruleSource.add("New Jersey");

		bindPosData();
		tdssDialog = new TimeSettingSelectionDialog(this);
		itidDialog = new InputTimeInfoDialog(this, this);
		GlobalInfo.getInstance().calendar_type = psService.Find().isCalendarType();
		
		btn_pos_save.setEnabled(false);
		btn_pos_undo.setEnabled(false);
		chooseAdapter.ScrollToRow(0);
		ChangeMode.mode = 1;
	}
	
	public OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (parent.getId()) {
			case R.id.lv_choose:
				Keyboard();
				chooseAdapter.ScrollToRow(position);
				switch (position) {
				case POS_SETTING:
					ll_pos_possetting.setVisibility(View.VISIBLE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindPosData();
					ChangeMode.mode = 1;
					et_store.requestFocus();
					break;
				case COMPANYINFO:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.VISIBLE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindStoreData();
					ChangeMode.mode = 7;
//					et_name.requestFocus();
					btn_cpy_save.setEnabled(false);
					btn_cpy_undo.setEnabled(false);
					btn_cpy_exit.setEnabled(true);
					break;
				case TAX_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.VISIBLE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindTaxSetting();
					ChangeMode.mode = 2;
					et_rate.requestFocus();
					btn_tax_add.setEnabled(true);
					btn_tax_save.setEnabled(false);
					btn_tax_undo.setEnabled(false);
					btn_tax_exit.setEnabled(true);
					break;
				case TARE_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.VISIBLE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindTareData();
					ChangeMode.mode = 3;
					tareAdapter.ScrollToRow(0);
					btn_ta_save.setEnabled(false);
					btn_ta_undo.setEnabled(false);
					break;
				case BOTTLE_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.VISIBLE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindBottleData();
					ChangeMode.mode = 4;
					bottleAdapter.ScrollToRow(0);
					btn_bo_save.setEnabled(false);
					btn_bo_undo.setEnabled(false);
					break;
				case HEADER_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.VISIBLE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindHeader();
					ChangeMode.mode = 5;
					headerAdapter.ScrollToRow(0);
					btn_header_save.setEnabled(false);
					btn_header_undo.setEnabled(false);
					break;
				case TRAILER_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.VISIBLE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindTrailer();
					ChangeMode.mode = 6;
					trailerAdapter.ScrollToRow(0);
					btn_trailer_save.setEnabled(false);
					btn_trailer_undo.setEnabled(false);
					break;
				case INVENTORY_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.VISIBLE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindInventory();
					ChangeMode.mode = 8;
					et_inv_server_ip.requestFocus();
					btn_inv_save.setEnabled(false);
					btn_inv_undo.setEnabled(false);
					btn_inv_exit.setEnabled(true);
					break;
				case PRINTER_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.VISIBLE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindPrinterData();
					ChangeMode.mode = 9;
					printerAdapter.ScrollToRow(0);
					btn_pr_save.setEnabled(false);
					btn_pr_undo.setEnabled(false);
					btn_pr_exit.setEnabled(true);
					break;
				case ROUND_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.VISIBLE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindRoundingData();
					ChangeMode.mode = 10;
					roundAdapter.ScrollToRow(0);
					btn_ro_save.setEnabled(false);
					btn_ro_undo.setEnabled(false);
					btn_ro_exit.setEnabled(true);
					break;
				case DVR_SETTING:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.VISIBLE);
					ll_pos_mult_coupon.setVisibility(View.GONE);
					ChangeMode.mode = 0;
					bindDVRSetting();
					ChangeMode.mode = 11;
					et_dvr_ip.requestFocus();
					btn_dvr_save.setEnabled(false);
					btn_dvr_undo.setEnabled(false);
					btn_dvr_exit.setEnabled(true);
					break;
				case MULT_COUPON:
					ll_pos_possetting.setVisibility(View.GONE);
					ll_pos_companyinfo.setVisibility(View.GONE);
					ll_pos_tax.setVisibility(View.GONE);
					ll_pos_tare.setVisibility(View.GONE);
					ll_pos_bottle.setVisibility(View.GONE);
					ll_pos_header.setVisibility(View.GONE);
					ll_pos_trailer.setVisibility(View.GONE);
					ll_pos_inventory.setVisibility(View.GONE);
					ll_pos_printer.setVisibility(View.GONE);
					ll_pos_rounding.setVisibility(View.GONE);
					ll_pos_dvr.setVisibility(View.GONE);
					ll_pos_mult_coupon.setVisibility(View.VISIBLE);
					ChangeMode.mode = 0;
//					bindDVRSetting();
					bindMultCouponSetting();
					GetMultCouponSettingDetail(0);
					mMultCouponAdapter.ScrollToRow(0);
					ChangeMode.mode = 12;
					btn_mult_coupon_add.setEnabled(true);
					btn_mult_coupon_save.setEnabled(false);
					btn_mult_coupon_undo.setEnabled(false);
					btn_mult_coupon_exit.setEnabled(true);
					break;
				}
				break;
			case R.id.lv_bottlelist:
				bottleAdapter.ScrollToRow(position);
				break;
			case R.id.lv_tarelist:
				tareAdapter.ScrollToRow(position);
				break;
			case R.id.lv_header:
				headerAdapter.ScrollToRow(position);
				break;
			case R.id.lv_trailer:
				trailerAdapter.ScrollToRow(position);
				break;
			case R.id.lv_printerlist:
				printerAdapter.ScrollToRow(position);
				break;
			case R.id.lv_roundlist:
				roundAdapter.ScrollToRow(position);
				break;
			case R.id.lv_taxlist:
				ChangeMode.mode = 0;
				taxAdapter.ScrollToRow(position);
				int taxNo = position + 1;
				if (taxService.checkTaxNo(taxNo)) {
					listTax = taxService.getTaxByNo(taxNo);
					bindTaxData();
					listTax = taxService.getAllTaxSetting();
				} else {
					bindTaxNull();
				}
				ChangeMode.mode = 2;
				break;
			case R.id.lv_mult_coupon_list:
				mMultCouponAdapter.ScrollToRow(position);
				ChangeMode.mode = 0;
				GetMultCouponSettingDetail(position);
				ChangeMode.mode = 12;
				break;
			}
		}
	};
	
	private void bindTaxSetting() {
		listTax = taxService.getAllTaxSetting();
		
		if (taxAdapter == null)
			taxAdapter = new SelectionAdapter(this, AdapterDataSourceBuilder.readValueFromTaxSettings(listTax));
		lv_tax_list.setAdapter(taxAdapter);
		if (listTax == null) {
			return;
		} else if (listTax.size() > 0) {
			bindTaxData();
			taxAdapter.ScrollToRow(0);
		} else if (listTax.size() == 0) {
			SetTaxEnabledFalse();
		}
	}

	private void bindTaxSettingByNo(int no) {
		listTax = taxService.getTaxByNo(no);
		if (listTax == null) {
			return;
		} else if (listTax.size() > 0) {
			bindTaxData();
		}
	}
	
	private void bindHeader(){
		listHeader = receiptService.FindALLHeader();
		if (listHeader == null) {
			return;
		}
		if (headerAdapter == null) {
			headerAdapter = new ReceiptSetupAdapter(this, listHeader);
			headerAdapter.setPage(ReceiptSetupAdapter.HEADER_EDITED);
			headerAdapter.setEditableListener(mEditableListener);
		}
		lv_header.setAdapter(headerAdapter);
	}
	
	private void bindTrailer(){
		listTrailer = receiptService.FindALLTrailer();
		if (listTrailer == null) {
			return;
		}
		if (trailerAdapter == null) {
			trailerAdapter = new ReceiptSetupAdapter(this, listTrailer);
			trailerAdapter.setPage(ReceiptSetupAdapter.TRAILER_EDITED);
			trailerAdapter.setEditableListener(mEditableListener);
		}
		lv_trailer.setAdapter(trailerAdapter);
	}
	
	private void bindInventory(){
		mInventorySetting = mInventorySettingService.FindData();
		if (mInventorySetting == null){
			InventorySetting mInventorySetting = new InventorySetting();
			mInventorySetting.setIs_onoff("0");
			mInventorySetting.setIs_server_ip("");
			mInventorySettingService.Insert(mInventorySetting);
			return ;
		}else{
			if(mInventorySetting.getIs_onoff().equals("0")){
				cb_inv_server_on_off.setChecked(false);
			}else{
				cb_inv_server_on_off.setChecked(true);
			}
			et_inv_server_ip.setText(mInventorySetting.getIs_server_ip());
		}
	}
	
	private void bindDVRSetting(){
		mDvrSetting = mDvrSettingService.FindData();
		if (mDvrSetting == null){
			DvrSetting mDvrSetting = new DvrSetting();
			mDvrSetting.setDs_onoff("0");
			mDvrSetting.setDs_dvr_ip("");
			mDvrSettingService.Insert(mDvrSetting);
			return;
		}else{
			if(mDvrSetting.getDs_onoff().equals("0")){
				cb_dvr_server_on_off.setChecked(false);
			}else{
				cb_dvr_server_on_off.setChecked(true);
			}
			et_dvr_ip.setText(mDvrSetting.getDs_dvr_ip());
			if (mDvrSetting.getDs_port() == 0) {
				et_dvr_port.setText("");
			}else {
				et_dvr_port.setText(String.valueOf(mDvrSetting.getDs_port()));
			}
		}
	}
	
	private void bindMultCouponSetting(){
		listMultCouponSetting = mMultCouponSettingService.findAll();
		if(mMultCouponAdapter == null){
			mMultCouponAdapter = new MultCouponAdapter(this,listMultCouponSetting);
			mMultCouponAdapter.setEditableListener(mEditableListener);
			lv_mult_coupon_list.setAdapter(mMultCouponAdapter);
		}else{
			mMultCouponAdapter.setSource(listMultCouponSetting);
			mMultCouponAdapter.notifyDataSetChanged();
		}
		
		
		if(listMultCouponSetting.size() <= 0){
			et_mult_coupon_name.setEnabled(false);
			et_mult_coupon_qty.setEnabled(false);
			et_mult_coupon_add_value.setEnabled(false);
			et_mult_coupon_max_value.setEnabled(false);
			et_mult_coupon_max_total.setEnabled(false);
			
			et_mult_coupon_name.setText("");
			et_mult_coupon_qty.setText("");
			et_mult_coupon_add_value.setText("");
			et_mult_coupon_max_value.setText("");
			et_mult_coupon_max_total.setText("");
			
			cb_mult_coupon_active.setChecked(false);
			cb_mult_coupon_active.setEnabled(false);
			btn_mult_coupon_delete.setEnabled(false);
		}else{
			et_mult_coupon_name.setEnabled(true);
			et_mult_coupon_qty.setEnabled(true);
			et_mult_coupon_add_value.setEnabled(true);
			et_mult_coupon_max_value.setEnabled(true);
			et_mult_coupon_max_total.setEnabled(true);
			cb_mult_coupon_active.setEnabled(true);
			btn_mult_coupon_delete.setEnabled(true);
		}
	}

	private void bindPrinterData(){
		listPrinter = mPrinterSettingService.FindAll();
		printerAdapter = new PrinterSetupAdapter(this, listPrinter);
		printerAdapter.setEditableListener(mEditableListener);
		lv_printerlist.setAdapter(printerAdapter);
	}
	
	private void bindRoundingData(){
		listRound = mRoundingSettingService.FindAll();
		roundAdapter = new RoundSetupAdapter(this, listRound);
		roundAdapter.setEditableListener(mEditableListener);
		lv_roundlist.setAdapter(roundAdapter);
	}
	
	private void bindPosData() {
		listPos = psService.getAllPosSetting();

		if (listPos == null) {
			return;
		}else if (listPos.size() <= 0) {
			result = psService.insertDefaultPos();
			listPos = psService.getAllPosSetting();
		}
		PosSetting mPosSetting = listPos.get(0);

		tv_id.setText(String.valueOf(mPosSetting.getPs_id()));
		et_store.setText(String.valueOf(mPosSetting.getStore_no()));
		et_station.setText(String.valueOf(mPosSetting.getPs_station()));
		et_openTime.setText(gettime(mPosSetting.getPs_open_time()));
		et_closeTime.setText(gettime(mPosSetting.getPs_close_time()));
		et_tareRate.setText(String.valueOf(mPosSetting.getPs_tare_rate()));
		et_fcMix.setText(String.valueOf(mPosSetting.getPs_fc_max()));
		btn_sign.setText(mPosSetting.getCurrency_sign());
		String ls_weight = mPosSetting.getPs_weight_unit();
		if (ls_weight == null || ls_weight.equals("")) {
			btn_unit.setText("g");
		} else {
			btn_unit.setText(mPosSetting.getPs_weight_unit());
		}
		int digits = mPosSetting.getPrice_digits();
		if (digits == 0) {
			btn_digits.setText("4");
		} else {
			btn_digits.setText(String.valueOf(digits));
		}
		
		if (mPosSetting.isOutOfPaper()) {
			cb_paper.setChecked(true);
		}else {
			cb_paper.setChecked(false);
		}
		
		if(mPosSetting.isCalcWeight()){
			cb_weight.setChecked(true);
		}else{
			cb_weight.setChecked(false);
		}
		
		if (mPosSetting.isCalendarType()) {
			cb_calendar.setChecked(true);
		}else {
			cb_calendar.setChecked(false);
		}
		
		if (mPosSetting.isNormUpce()) {
			cb_norm.setChecked(true);
		}else {
			cb_norm.setChecked(false);
		}
		
		if (mPosSetting.isMGNetwork()) {
			cb_networkOnoff.setChecked(true);
		}else {
			cb_networkOnoff.setChecked(false);
		}
		
		if (mPosSetting.isShowDesc()) {
			cb_showDesc.setChecked(true);
		}else {
			cb_showDesc.setChecked(false);
		}
		
//		if (mPosSetting.isDVR()) {
//			cb_dvr.setChecked(true);
//		}else {
//			cb_dvr.setChecked(false);
//		}
//		
		if (mPosSetting.isZeroBaln()) {
			cb_zero_baln.setChecked(true);
		}else {
			cb_zero_baln.setChecked(false);
		}
		
		if (mPosSetting.isZeroPrice()) {
			cb_zero_price.setChecked(true);
		}else {
			cb_zero_price.setChecked(false);
		}
		
		int fs_tax_rule = mPosSetting.getPs_fs_tax_rule();
		if (fs_tax_rule == 1) {
			btn_fs_tax_rule.setText("Illinois");
		} else {
			btn_fs_tax_rule.setText("New Jersey");
		}
		
		cb_label_printer.setChecked(mPosSetting.getIs_prctag_printer());
		if (mPosSetting.getIs_prctag_printer()) {
			btn_label_printer.setVisibility(View.VISIBLE);
//			if (mPosSetting.getPs_prctag_printer() == 999) {
//				btn_label_printer.setText("Default Printer");
//				btn_label_printer.setTag(new PrinterSetting(999, "Default Printer", "", 0));
//			}
			PrinterSetting printerSetting = mPrinterSettingService.FindByID(mPosSetting.getPs_prctag_printer());
			if (printerSetting != null) {
				btn_label_printer.setText(printerSetting.getPs_name());
				btn_label_printer.setTag(printerSetting);
			}
		}
		
		cb_rounding.setChecked(mPosSetting.getIs_rounding());
		if (mPosSetting.getIs_rounding()) {
			btn_rounding.setVisibility(View.VISIBLE);
			RoundingSetting roundingSetting = mRoundingSettingService.FindByID(mPosSetting.getPs_rounding());
			if (roundingSetting != null) {
				btn_rounding.setText(roundingSetting.getRs_name());
				btn_rounding.setTag(roundingSetting);
			}
		}
		
		if (mPosSetting.isPrintServic()) {
			cb_printService.setChecked(true);
		}else {
			cb_printService.setChecked(false);
		}
		
		if (mPosSetting.isSigHost()) {
			cb_sig_host.setChecked(true);
		}else {
			cb_sig_host.setChecked(false);
		}
		
		if (mPosSetting.isSaveSig()) {
			cb_save_sig.setChecked(true);
		}else {
			cb_save_sig.setChecked(false);
		}
		et_sig_min.setText(String.format("%.2f",mPosSetting.getPs_sig_min()));
		
		PrinterSetting receiptPrinter = mPrinterSettingService.FindByID(mPosSetting.getPs_receipt_pnt());
		if (receiptPrinter != null) {
			btn_receipt_printer.setText(receiptPrinter.getPs_name());
			btn_receipt_printer.setTag(receiptPrinter);
		}else {
			btn_receipt_printer.setText("Default Printer");
			btn_receipt_printer.setTag(new PrinterSetting(0, "Default Printer", "", 0));
		}
	}

	private void bindTareData() {
		listTare = tareService.FindAll();
		tareAdapter = new TareSetupAdapter(this, listTare);
		tareAdapter.setEditableListener(mEditableListener);
		lv_tarelist.setAdapter(tareAdapter);
	}

	private void bindBottleData() {
		listBottle = bottleService.FindAll();
		bottleAdapter = new BottleSetupAdapter(this, listBottle);
		bottleAdapter.setEditableListener(mEditableListener);
		lv_bottlelist.setAdapter(bottleAdapter);
	}

	private void bindStoreData() {
		listStore = storeService.getStoreSetting();
		if (listStore == null || listStore.size() <= 0) {
			String sql = "insert into t_store_setting values(1,'',null,null,null,null,null,null,null,null,null,"
					+ "strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'),null)";
			storeService.AddStoreSetting(sql);
			StoreSetting ss = new StoreSetting(1, null, null, null, null, null, null, null, null, null, null, null);
			ss.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
			ss.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
			if (GlobalInfo.getInstance().isMGNetwork) {
				mMGCreateFiles.createrFile(ss.toMGData("INSERT"));
			}
			et_businessName.setText("");
			et_name.setText("");
			et_name.setTag(1);
			et_desc.setText("");
			et_address_1.setText("");
			et_address_2.setText("");
			et_city.setText("");
			et_zip.setText("");
			et_telephone.setText("");
			et_fax.setText("");
			et_website.setText("");
			btn_state.setText("");
		} else {
			et_businessName.setText(listStore.get(0).getSs_business_name());
			et_name.setText(listStore.get(0).getSs_name());
			et_name.setTag(listStore.get(0).getSs_id());
			et_desc.setText(listStore.get(0).getSs_other_name());
			et_address_1.setText(listStore.get(0).getSs_address_1());
			et_address_2.setText(listStore.get(0).getSs_address_2());
			et_city.setText(listStore.get(0).getSs_city());
			et_zip.setText(listStore.get(0).getSs_zip());
			et_telephone.setText(listStore.get(0).getSs_telephone());
			et_fax.setText(listStore.get(0).getSs_fax());
			et_website.setText(listStore.get(0).getSs_website());
			btn_state.setText(listStore.get(0).getSs_state());
		}
	}

	private void bindStateData() {
		listStates = statesService.getStates();
	}

	private void bindTaxData() {
		et_rate.setText(String.valueOf(listTax.get(0).getTx_rate()));
		String sv = String.format("%.2f", listTax.get(0).getTx_start_amt());
		et_startvalue.setText(sv);
		et_round.setText(String.valueOf(listTax.get(0).getTx_plus_round()));
		
		boolean by_line = listTax.get(0).isTx_by_line();
		cb_byline.setChecked(by_line);
		boolean vat = listTax.get(0).isTx_vat();
		cb_vat.setChecked(vat);
		SetTaxEnabledTrue();
	}

	private void bindTaxNull() {
		et_rate.setText("");
		et_startvalue.setText("");
		et_round.setText("");
		cb_byline.setChecked(false);
		cb_vat.setChecked(false);
		et_rate.requestFocus();
	}

	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			switch (buttonView.getId()) {
			case R.id.cb_label_printer:
				if (isChecked) {
					btn_label_printer.setVisibility(View.VISIBLE);
				}else {
					btn_label_printer.setText("");
					btn_label_printer.setTag(null);
					btn_label_printer.setVisibility(View.GONE);
				}
				break;
			case R.id.cb_rounding:
				if (isChecked) {
					btn_rounding.setVisibility(View.VISIBLE);
				}else {
					btn_rounding.setText("");
					btn_rounding.setTag(null);
					btn_rounding.setVisibility(View.GONE);
				}
				break;
			}
			if (ChangeMode.mode == 2) {
				SetEnabledFalse(2);
			}else if (ChangeMode.mode == 1) {
				SetEnabledFalse(1);
			}else if (ChangeMode.mode == 8) {
				SetEnabledFalse(8);
			}else if (ChangeMode.mode == 11) {
				SetEnabledFalse(11);
			}else if (ChangeMode.mode == 12){
				SetEnabledFalse(12);
			}
		}
	};

	private TextWatcher watcher = new TextWatcher() {

		public void afterTextChanged(Editable s) {
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

			if (ChangeMode.mode == 1) {
				SetEnabledFalse(1);
			} else if (ChangeMode.mode == 2) {
				SetEnabledFalse(2);
			} else if (ChangeMode.mode == 7) {
				SetEnabledFalse(7);
			}else if (ChangeMode.mode == 8) {
				SetEnabledFalse(8);
			}else if (ChangeMode.mode == 11) {
				SetEnabledFalse(11);
			}else if (ChangeMode.mode == 12) {
				SetEnabledFalse(12);
			}
		}
	};

	private EditableListener mEditableListener = new EditableListener() {

		public void onEdited(String str) {
			if (str.equals(TareSetupAdapter.TARE_EDITED)) {
				SetEnabledFalse(3);
			} else if (str.equals(BottleSetupAdapter.BOTTLE_EDITED)) {
				SetEnabledFalse(4);
			} else if (str.equals(ReceiptSetupAdapter.HEADER_EDITED)) {
				SetEnabledFalse(5);
			} else if (str.equals(ReceiptSetupAdapter.TRAILER_EDITED)) {
				SetEnabledFalse(6);
			} else if (str.equals(PrinterSetupAdapter.PRINT_EDITED)) {
				SetEnabledFalse(9);
			} else if (str.equals(RoundSetupAdapter.ROUND_EDITED)) {
				SetEnabledFalse(10);
			} else if (str.equals(MultCouponAdapter.MULTCOUPON_EDITED)){
				SetEnabledFalse(12);
			}
		}
	};

	public void SetEnabledFalse(int mode) {
		lv_choose.setEnabled(false);
		switch (mode) {
		case 1: // pos setting
			btn_pos_save.setEnabled(true);
			btn_pos_undo.setEnabled(true);
			btn_pos_exit.setEnabled(false);
			break;
		case 2: // tax setting
			btn_tax_add.setEnabled(false);
			btn_tax_save.setEnabled(true);
			btn_tax_undo.setEnabled(true);
			btn_tax_exit.setEnabled(false);
			lv_tax_list.setEnabled(false);
			break;
		case 3: // tare
			btn_ta_add.setEnabled(false);
			btn_ta_delete.setEnabled(false);
			btn_ta_exit.setEnabled(false);
			btn_ta_save.setEnabled(true);
			btn_ta_undo.setEnabled(true);
			break;
		case 4: // bottle
			btn_bo_add.setEnabled(false);
			btn_bo_delete.setEnabled(false);
			btn_bo_exit.setEnabled(false);
			btn_bo_save.setEnabled(true);
			btn_bo_undo.setEnabled(true);
			break;
		case 5: // header
			iv_h_copy.setEnabled(false);
			btn_header_add.setEnabled(false);
			btn_header_delete.setEnabled(false);
			btn_header_exit.setEnabled(false);
			btn_header_save.setEnabled(true);
			btn_header_undo.setEnabled(true);
			break;
		case 6: // trailer
			btn_trailer_add.setEnabled(false);
			btn_trailer_delete.setEnabled(false);
			btn_trailer_exit.setEnabled(false);
			btn_trailer_save.setEnabled(true);
			btn_trailer_undo.setEnabled(true);
			break;
		case 7:	// company
			btn_cpy_save.setEnabled(true);
			btn_cpy_undo.setEnabled(true);
			btn_cpy_exit.setEnabled(false);
			break;
		case 8:	// Inventory
			btn_inv_save.setEnabled(true);
			btn_inv_undo.setEnabled(true);
			btn_inv_exit.setEnabled(false);
			break;
		case 9:	//Printer
			btn_pr_add.setEnabled(false);
			btn_pr_delete.setEnabled(false);
			btn_pr_exit.setEnabled(false);
			btn_pr_save.setEnabled(true);
			btn_pr_undo.setEnabled(true);
			break;
		case 10: //Rounding
			btn_ro_add.setEnabled(false);
			btn_ro_delete.setEnabled(false);
			btn_ro_exit.setEnabled(false);
			btn_ro_save.setEnabled(true);
			btn_ro_undo.setEnabled(true);
			break;
		case 11: //DVR
			btn_dvr_save.setEnabled(true);
			btn_dvr_undo.setEnabled(true);
			btn_dvr_exit.setEnabled(false);
			break;
		case 12: //MultCoupon
			btn_mult_coupon_add.setEnabled(false);
			btn_mult_coupon_exit.setEnabled(false);
			btn_mult_coupon_save.setEnabled(true);
			btn_mult_coupon_undo.setEnabled(true);
			btn_mult_coupon_delete.setEnabled(false);
			lv_mult_coupon_list.setEnabled(false);
			break;
		}
	}

	public void SetEnabledTrue(int mode) {
		switch (mode) {
		case 1:
			btn_pos_save.setEnabled(false);
			btn_pos_undo.setEnabled(false);
			btn_pos_exit.setEnabled(true);
			break;
		case 2:
			btn_tax_add.setEnabled(true);
			btn_tax_save.setEnabled(false);
			btn_tax_undo.setEnabled(false);
			btn_tax_exit.setEnabled(true);
			lv_tax_list.setEnabled(true);
			break;
		case 7:
			btn_cpy_save.setEnabled(false);
			btn_cpy_undo.setEnabled(false);
			btn_cpy_exit.setEnabled(true);
			break;
		case 8:
			btn_inv_save.setEnabled(false);
			btn_inv_undo.setEnabled(false);
			btn_inv_exit.setEnabled(true);
			break;
		case 11: //DVR
			btn_dvr_save.setEnabled(false);
			btn_dvr_undo.setEnabled(false);
			btn_dvr_exit.setEnabled(true);
			break;
		case 12: //MultCoupon
			btn_mult_coupon_add.setEnabled(true);
			btn_mult_coupon_exit.setEnabled(true);
			btn_mult_coupon_save.setEnabled(false);
			btn_mult_coupon_undo.setEnabled(false);
			btn_mult_coupon_delete.setEnabled(true);
			lv_mult_coupon_list.setEnabled(true);
			break;
		}
	}

	public OnClickListener onImageClickListener = new OnClickListener() {

		String id, store, station, open_time, close_time, tare_rate, fc_mix,
				weight_unit, price_digits,sign,paper,rule,sig_min;
		String rate, start_value, round,weight,calendar_type,norm_upce,network_onoff,show_desc,
			dvr,zero_baln,zero_price,print_service,sig_host,sig_local;
		int tax_no,fs_tax_rule,label_printer,rounding,receipt_printer;
		boolean by_line = false,vat = false;

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.iv_h_copy:
				listStore = storeService.getStoreSetting();
				if (listStore.size() <= 0)
					return;
				if (listStore.get(0).getSs_name().trim().equals(""))
					return;
				is_copy = true;
				msgType = 1;
				ShowMessage(
						"Do you want to copy store info into the receipt header?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);

				break;
			case R.id.btn_pos_save: // PosSetting
				ChangeMode.mode = 0;
				posSettingx = new PosSetting();

				if (validation() < 0) {
					return;
				}

				id = tv_id.getText().toString();
				store = et_store.getText().toString();
				station = et_station.getText().toString();
				open_time = et_openTime.getText().toString();
				close_time = et_closeTime.getText().toString();
				tare_rate = et_tareRate.getText().toString();
				double tareRare = Double.valueOf(tare_rate);
				if (tareRare == 0) {
					tareRare = 0.01;
				}
				tare_rate = String.format("%.3f", tareRare);

				fc_mix = et_fcMix.getText().toString();
				weight_unit = btn_unit.getText().toString();
				price_digits = btn_digits.getText().toString();
				sign = btn_sign.getText().toString();
				rule = btn_fs_tax_rule.getText().toString();
				if (cb_paper.isChecked()) {
					paper = "1";
				}else {
					paper = "0";
				}
				if(cb_weight.isChecked()){
					weight = "1";
				}else{
					weight = "0";
				}
				if (cb_calendar.isChecked()) {
					calendar_type = "1";
				}else {
					calendar_type = "0";
				}
				if (cb_norm.isChecked()) {
					norm_upce = "1";
				}else {
					norm_upce = "0";
				}
				if (cb_networkOnoff.isChecked()) {
					network_onoff = "1";
				}else {
					network_onoff = "0";
				}
				if (cb_showDesc.isChecked()) {
					show_desc = "1";
				}else {
					show_desc = "0";
				}
//				if (cb_dvr.isChecked()) {
//					dvr = "1";
//				}else {
					dvr = "0";
//				}
				if (cb_zero_baln.isChecked()) {
					zero_baln = "1";
				}else {
					zero_baln = "0";
				}
				if (cb_zero_price.isChecked()) {
					zero_price = "1";
				}else {
					zero_price = "0";
				}
				if (rule.equals("Illinois")) {
					fs_tax_rule = 1;
				}else {
					fs_tax_rule = 2;
				}
				if (cb_label_printer.isChecked()) {
					if (btn_label_printer.getText().toString().equals("")) {
						ShowMessage("Please select a printer.",
								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
						return;
					}
					PrinterSetting mPrinter = (PrinterSetting) btn_label_printer.getTag();
					if (mPrinterSettingService.CheckPrinter(mPrinter.getPs_id())) {
						label_printer = -1;
					} else {
						label_printer = mPrinter.getPs_id();
					}
				}else {
					label_printer = -1;
				}
				
				if (cb_rounding.isChecked()) {
					if (btn_rounding.getText().toString().equals("")) {
						ShowMessage("Please select a rounding.",
								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
						return;
					}
					RoundingSetting mRounding = (RoundingSetting) btn_rounding.getTag();
					if (mRoundingSettingService.CheckRounding(mRounding.getRs_id())) {
						rounding = -1;
					}else {
						rounding = mRounding.getRs_id();
					}
				}else {
					rounding = -1;
				}
				
				if (cb_printService.isChecked()) {
					print_service = "1";
				}else {
					print_service = "0";
				}
				
				if (cb_sig_host.isChecked()) {
					sig_host = "1";
				}else {
					sig_host = "0";
				}
				
				if (cb_save_sig.isChecked()) {
					sig_local = "1";
				}else {
					sig_local = "0";
				}
				sig_min = et_sig_min.getText().toString();
				sig_min = String.format("%.2f", Double.valueOf(sig_min));

				if (btn_receipt_printer.getText().toString().equals("")) {
					ShowMessage("Please select a receipt printer.",
							mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
					return;
				}
				PrinterSetting receipt = (PrinterSetting) btn_receipt_printer.getTag();
				if (mPrinterSettingService.CheckPrinter(receipt.getPs_id())) {
					receipt_printer = 0;
				} else {
					receipt_printer = receipt.getPs_id();
				}
				
				posSettingx.setPs_id(Integer.valueOf(id));
				posSettingx.setStore_no(Integer.valueOf(store));
				posSettingx.setPs_station(Integer.valueOf(station));
				posSettingx.setPs_open_time(gettime_short(open_time));
				posSettingx.setPs_close_time(gettime_short(close_time));
				posSettingx.setPs_tare_rate(Double.valueOf(tare_rate));
				posSettingx.setPs_fc_max(Integer.valueOf(fc_mix));
				posSettingx.setPs_weight_unit(weight_unit);
				posSettingx.setPrice_digits(Integer.valueOf(price_digits));
				posSettingx.setCurrency_sign(sign);
				posSettingx.setOutofpaper(paper);
				posSettingx.setCalculate_weight(weight);
				posSettingx.setCalendar_type(calendar_type);
				posSettingx.setNorm_upce(norm_upce);
				posSettingx.setNetwork_onoff(network_onoff);
				posSettingx.setShow_desc(show_desc);
				posSettingx.setDvr(dvr);
				posSettingx.setZero_baln(zero_baln);
				posSettingx.setZero_price(zero_price);
				posSettingx.setPs_fs_tax_rule(fs_tax_rule);
				posSettingx.setPs_prctag_printer(label_printer);
				posSettingx.setPs_rounding(rounding);
				posSettingx.setPs_print_service(print_service);
				posSettingx.setPs_sig_host(sig_host);
				posSettingx.setPs_sig_local(sig_local);
				posSettingx.setPs_sig_min(Double.valueOf(sig_min));
				posSettingx.setPs_receipt_pnt(receipt_printer);
				posSettingx.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				posSettingx.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				
				int oldStation = psService.getAllPosSetting().get(0).getPs_station();
				if (Integer.valueOf(station) != oldStation) {
					msgType = 5;
					ShowMessage("Do you really want to change the station?", mMessageDialog.MESSAGE_BOX_YES_NO,
							mMessageDialog.ICON_INFO);
				}else {
					result = psService.updatePosSetting(posSettingx);
					GlobalInfo.getInstance().isMGNetwork = psService.getAllPosSetting().get(0).isMGNetwork();
					GlobalInfo.getInstance().calendar_type = psService.getAllPosSetting().get(0).isCalendarType();
					GlobalInfo.getInstance().isZeroBaln = psService.getAllPosSetting().get(0).isZeroBaln();
					GlobalInfo.getInstance().isZeroPrice = psService.getAllPosSetting().get(0).isZeroPrice();
					GlobalInfo.getInstance().CurrencySymbol = psService.getAllPosSetting().get(0).getCurrency_sign();
					GlobalInfo.getInstance().isPrintServiceItem = psService.getAllPosSetting().get(0).isPrintServic();
					GlobalInfo.getInstance().isSigHost = psService.getAllPosSetting().get(0).isSigHost();
					GlobalInfo.getInstance().isSaveSig = psService.getAllPosSetting().get(0).isSaveSig();
					GlobalInfo.getInstance().sigMinValue = psService.getAllPosSetting().get(0).getPs_sig_min();
					GlobalInfo.getInstance().receiptPrinter = psService.getAllPosSetting().get(0).getPs_receipt_pnt();
					
					if (result && GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(posSettingx.toMGData("UPDATE"));
					}
					ShowMessage(result);
					bindPosData();
					ChangeMode.mode = 1;
					SetEnabledTrue(1);
					Keyboard();
					lv_choose.setEnabled(true);
				}
				
				break;
			case R.id.btn_pos_undo:
				ChangeMode.mode = 0;
				bindPosData();
				ChangeMode.mode = 1;
				SetEnabledTrue(1);
				Keyboard();
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_pos_exit:
				ShowExit();
				break;
			case R.id.btn_tax_save: // TaxSetting
				ChangeMode.mode = 0;
				if (taxValitation() < 0) {
					return;
				}
				int index = taxAdapter.GetSelectedIndex();
				tax_no = index + 1;
				rate = et_rate.getText().toString();
				start_value = et_startvalue.getText().toString();
				round = et_round.getText().toString();

				rate = String.format("%.3f", Double.valueOf(rate));
				start_value = String.format("%.2f", Double.valueOf(start_value));
				round = String.format("%.3f", Double.valueOf(round));
				if (cb_byline.isChecked()) {
					by_line = true;
				} else {
					by_line = false;
				}
				if (cb_vat.isChecked()) {
					vat = true;
				} else {
					vat = false;
				}

				TaxSetting ts = new TaxSetting();
				ts.setTx_no(tax_no);
				ts.setTx_rate(Double.valueOf(rate));
				ts.setTx_start_amt(Double.valueOf(start_value));
				ts.setTx_plus_round(Double.valueOf(round));
				ts.setTx_by_line(by_line);
				ts.setTx_vat(vat);
				ts.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				ts.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));

				if (taxService.checkTaxNo(tax_no) == false) {
					result = taxService.insertTaxSetting(ts);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(ts.toMGData("INSERT"));
					}
				} else {
					result = taxService.updateTaxSetting(ts);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(ts.toMGData("UPDATE"));
					}
				}
				ShowMessage(result);
				bindTaxSettingByNo(tax_no);
				ChangeMode.mode = 2;
				SetEnabledTrue(2);
				Keyboard();
				lv_choose.setEnabled(true);
				btn_tax_add.setEnabled(true);
				btn_tax_save.setEnabled(false);
				btn_tax_undo.setEnabled(false);
				btn_tax_exit.setEnabled(true);
				break;
			case R.id.btn_tax_add: // tax add
				TaxSetting tax = new TaxSetting();
				listTax = taxService.getAllTaxSetting();
				if (listTax.size() >= 6)
					return;

				int Value = getNextTaxNo(listTax);

				if (listTax.size() == 0) {
					tax.setTx_no(1);
				} else if (listTax.size() > 0) {
					tax.setTx_no(Value);
				}
				tax.setTx_start_amt(0.10);
				tax.setTx_plus_round(0.005);
				listTax.add(tax);
				sortList(listTax);
				et_rate.setText("");
				et_startvalue.setText("0.10");
				et_round.setText("0.005");
				cb_byline.setChecked(false);
				cb_vat.setChecked(false);
				et_rate.requestFocus();

				SetTaxEnabledTrue();
				lv_tax_list.setEnabled(false);
				taxAdapter.setSource(AdapterDataSourceBuilder.readValueFromTaxSettings(listTax));
				taxAdapter.notifyDataSetChanged();
				taxAdapter.ScrollToRow(Value - 1);
				lv_choose.setEnabled(false);
				btn_tax_add.setEnabled(false);
				btn_tax_save.setEnabled(true);
				btn_tax_undo.setEnabled(true);
				btn_tax_exit.setEnabled(false);
				break;
			case R.id.btn_tax_undo:
				ChangeMode.mode = 0;
				bindTaxSetting();
				listTax = taxService.getAllTaxSetting();
				taxAdapter.setSource(AdapterDataSourceBuilder.readValueFromTaxSettings(listTax));
				if (listTax == null) {
					return;
				} else if (listTax.size() > 0) {
					bindTaxData();
					taxAdapter.ScrollToRow(0);
				} else if (listTax.size() == 0) {
					SetTaxEnabledFalse();
				}
				ChangeMode.mode = 2;
				et_rate.requestFocus();
				lv_choose.setEnabled(true);
				btn_tax_add.setEnabled(true);
				btn_tax_save.setEnabled(false);
				btn_tax_undo.setEnabled(false);
				btn_tax_exit.setEnabled(true);
				lv_tax_list.setEnabled(true);
				break;
			case R.id.btn_tax_exit:
				ShowExit();
				break;
			}
		}
	};

	public int getNextTaxNo(List<TaxSetting> list) {

		int tax_no = 1;
		for (int i = 0; i < list.size(); i++) {
			int j = list.get(i).getTx_no();
			if (j != i + 1) {
				tax_no = i + 1;
				break;
			}
			tax_no = list.size() + 1;
		}
		return tax_no;
	}

	public List<TaxSetting> sortList(List<TaxSetting> list) {
		if (!list.isEmpty()) {
			Collections.sort(list, new Comparator<TaxSetting>() {

				public int compare(TaxSetting tax1, TaxSetting tax2) {
					return (String.valueOf(tax1.getTx_no()).compareTo(String
							.valueOf(tax2.getTx_no())));
				}
			});
		}
		return list;
	}

	public OnClickListener onClickListener = new OnClickListener() {
		TareEditDialog mTareEditDialog = null;
		BottleEditDialog mBottleEditDialog = null;
		ReceiptEditDialog mReceiptEditDialog = null;
		PrinterEditDialog mPrinterEditDialog = null;
		RoundEditDialog mRoundEditDialog = null;
		StringSourceDialog mStringSourceDialog = null;
		
		String ls_desc,ls_value,type;
		String on_off,ip,port;
		int row,tare_no,bottle_no,printer_no,round_no;
		public void onClick(View view) {

			switch (view.getId()) {
			//tare
			case R.id.btn_ta_add:
				TareSetting ta = new TareSetting();
				ta.setType("I");
				ta.setTa_no(tareService.getMaxTareNo() + 1);
//				ta.setTa_desc("");
				ta.setNo(listTare.size() + 1);
				
				mTareEditDialog = new TareEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mTareEditDialog.setSource(listTare, ta, "add");
				mTareEditDialog.show();
				break;
			case R.id.btn_ta_delete:
				if (listTare.size() == 0)
					return;
				row = tareAdapter.GetSelectedIndex();
				tare_no = listTare.get(row).getTa_no();
				if (tareService.ItemHaveTare(tare_no)) {
					ShowMessage(
							"The tare setting cannot be removed because it is linked to items.",
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
					return;
				}
				msgType = 3;
				ShowMessage("Do you want to delete this tare?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_ta_edit:
				if(listTare.size() == 0) return;
				row = tareAdapter.GetSelectedIndex();
				TareSetting mTareSetting = listTare.get(row);
				
				mTareEditDialog = new TareEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mTareEditDialog.setSource(listTare, mTareSetting, "edit");
				mTareEditDialog.show();
				break;
			case R.id.btn_ta_save:
				TareSetting ta1 = new TareSetting();
				for (int i = 0; i < listTare.size(); i++) {
					ls_desc = listTare.get(i).getTa_desc().trim();
					ls_value = String.valueOf(listTare.get(i).getTa_value());
					tare_no = listTare.get(i).getTa_no();
					type = listTare.get(i).getType();
					if (listTare.get(i).isEdit()) {
						ta1.setTa_no(tare_no);
						ta1.setTa_desc(ls_desc);
						ta1.setTa_value(Integer.valueOf(ls_value));
						ta1.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						ta1.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						
						if (type == "I") {
							result = tareService.insertTareSetting(ta1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(ta1.toMGData("INSERT"));
							}
						} else {
							result = tareService.updateTareSetting(ta1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(ta1.toMGData("UPDATE"));
							}
						}
					}
				}
				if (result) {
					ShowMessage(result);
					listTare = tareService.FindAll();
					tareAdapter.setSource(listTare);
					tareAdapter.notifyDataSetChanged();
					Keyboard();
					btn_ta_add.setEnabled(true);
					btn_ta_delete.setEnabled(true);
					btn_ta_exit.setEnabled(true);
					btn_ta_save.setEnabled(false);
					btn_ta_undo.setEnabled(false);
				}
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_ta_undo:
				listTare = tareService.FindAll();
				tareAdapter.setSource(listTare);
				tareAdapter.ScrollToRow(0);
				
				btn_ta_add.setEnabled(true);
				btn_ta_delete.setEnabled(true);
				btn_ta_exit.setEnabled(true);
				btn_ta_save.setEnabled(false);
				btn_ta_undo.setEnabled(false);
				lv_choose.setEnabled(true);
				break;
			//bottle
			case R.id.btn_bo_add:
				BottleSetting bo = new BottleSetting();
				bo.setType("I");
				bo.setBo_no(bottleService.getMaxBottleNo() + 1);
//				bo.setBo_desc("");
				bo.setNo(listBottle.size() + 1);
				
				mBottleEditDialog = new BottleEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mBottleEditDialog.setSource(listBottle, bo, "add");
				mBottleEditDialog.show();
				break;
			case R.id.btn_bo_delete:
				if (listBottle.size() == 0)
					return;
				row = bottleAdapter.GetSelectedIndex();
				bottle_no = listBottle.get(row).getBo_no();
				if (bottleService.ItemHaveBottle(bottle_no)) {
					ShowMessage(
							"The bottle setting cannot be removed because it is linked to items.",
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
					return;
				}
				msgType = 4;
				ShowMessage("Do you want to delete this bottle?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_bo_edit:
				if(listBottle.size() == 0) return;
				row = bottleAdapter.GetSelectedIndex();
				BottleSetting mBottleSetting = listBottle.get(row);
				
				mBottleEditDialog = new BottleEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mBottleEditDialog.setSource(listBottle, mBottleSetting, "edit");
				mBottleEditDialog.show();
				break;
			case R.id.btn_bo_save:
				for (int i = 0; i < listBottle.size(); i++) {
					BottleSetting bs1 = listBottle.get(i);
					ls_desc = listBottle.get(i).getBo_desc().trim();
					ls_value = String.valueOf(listBottle.get(i).getBo_amt());
					bottle_no = listBottle.get(i).getBo_no();
					type = listBottle.get(i).getType();
					if (listBottle.get(i).isEdit()) {
						bs1.setBo_no(bottle_no);
						bs1.setBo_desc(ls_desc);
						bs1.setBo_amt(Double.valueOf(ls_value));
						bs1.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						bs1.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						
						if (type == "I") {
							result = bottleService.insertBottleSetting(bs1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(bs1.toMGData("INSERT"));
							}
						} else {
							result = bottleService.updateBottleSetting(bs1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(bs1.toMGData("UPDATE"));
							}
						}
					}
				}
				if (result) {
					ShowMessage(result);
					listBottle = bottleService.FindAll();
					bottleAdapter.setSource(listBottle);
					bottleAdapter.notifyDataSetChanged();
					Keyboard();
					btn_bo_add.setEnabled(true);
					btn_bo_delete.setEnabled(true);
					btn_bo_exit.setEnabled(true);
					btn_bo_save.setEnabled(false);
					btn_bo_undo.setEnabled(false);
				}
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_bo_undo:
				listBottle = bottleService.FindAll();
				bottleAdapter.setSource(listBottle);
				bottleAdapter.ScrollToRow(0);
				
				btn_bo_add.setEnabled(true);
				btn_bo_delete.setEnabled(true);
				btn_bo_exit.setEnabled(true);
				btn_bo_save.setEnabled(false);
				btn_bo_undo.setEnabled(false);
				lv_choose.setEnabled(true);
				break;
			//header
			case R.id.btn_header_add:
				Receipt header = new Receipt();
				header.setId(receiptService.getMaxReceiptNo());
				header.setType("I");
				header.setMsg("");
				header.setLarge("N");
				header.setJustify("L");
				
				mReceiptEditDialog = new ReceiptEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mReceiptEditDialog.setSource(listHeader, header, "add", HEADER_RETURN);
				mReceiptEditDialog.show();
				break;
			case R.id.btn_header_delete:
				if (listHeader.size() == 0)
					return;
				
				row = headerAdapter.GetSelectedIndex();
				if (listHeader.get(row).getId() == 0) return;
				msgType = 1;
				ShowMessage("Do you want to delete this receipt header?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_header_edit:
				if(listHeader.size() == 0) return;
				row = headerAdapter.GetSelectedIndex();
				Receipt mHeader = listHeader.get(row);
				
				if (mHeader.getId() == 0) return;
				mReceiptEditDialog = new ReceiptEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mReceiptEditDialog.setSource(listHeader, mHeader, "edit", HEADER_RETURN);
				mReceiptEditDialog.show();
				break;
			case R.id.btn_header_save:
				for (int i = 0; i < listHeader.size(); i++) {
					Receipt header1 = listHeader.get(i);
					ls_value = listHeader.get(i).getMsg();
					type = listHeader.get(i).getType();
					if (listHeader.get(i).isEdit()) {
						header1.setType("H");
						header1.setMsg(ls_value);
						header1.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						header1.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						if (type.equals("I")) {
							result = receiptService.insertReceipt(header1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(header1.toMGData("INSERT"));
							}
						} else {
							result = receiptService.updateReceipt(header1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(header1.toMGData("UPDATE"));
							}
						}
					}
				}
				if (result) {
					ShowMessage(result);
					listHeader = receiptService.FindALLHeader();
					headerAdapter.setSource(listHeader);
					headerAdapter.notifyDataSetChanged();
					Keyboard();
					btn_header_add.setEnabled(true);
					btn_header_delete.setEnabled(true);
					btn_header_exit.setEnabled(true);
					btn_header_save.setEnabled(false);
					btn_header_undo.setEnabled(false);
					iv_h_copy.setEnabled(true);
				}
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_header_undo:
				listHeader = receiptService.FindALLHeader();
				headerAdapter.setSource(listHeader);
				headerAdapter.ScrollToRow(0);
				
				btn_header_add.setEnabled(true);
				btn_header_delete.setEnabled(true);
				btn_header_exit.setEnabled(true);
				btn_header_save.setEnabled(false);
				btn_header_undo.setEnabled(false);
				lv_choose.setEnabled(true);
				break;
			//trailer
			case R.id.btn_trailer_add:
				Receipt trailer = new Receipt();
				trailer.setId(receiptService.getMaxReceiptNo());
				trailer.setType("I");
				trailer.setMsg("");
				trailer.setLarge("N");
				trailer.setJustify("L");
				
				mReceiptEditDialog = new ReceiptEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mReceiptEditDialog.setSource(listTrailer, trailer, "add", TRAILER_RETURN);
				mReceiptEditDialog.show();
				break;
			case R.id.btn_trailer_delete:
				if (listTrailer.size() == 0)
					return;
				msgType = 2;
				ShowMessage("Do you want to delete this receipt trailer?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_trailer_edit:
				if(listTrailer.size() == 0) return;
				row = trailerAdapter.GetSelectedIndex();
				Receipt mTrailer = listTrailer.get(row);
				
				mReceiptEditDialog = new ReceiptEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mReceiptEditDialog.setSource(listTrailer, mTrailer, "edit", TRAILER_RETURN);
				mReceiptEditDialog.show();
				break;
			case R.id.btn_trailer_save:
				for (int i = 0; i < listTrailer.size(); i++) {
					Receipt trailer1 = listTrailer.get(i);
					ls_value = listTrailer.get(i).getMsg();
					type = listTrailer.get(i).getType();
					if (listTrailer.get(i).isEdit()) {
						trailer1.setType("T");
						trailer1.setMsg(ls_value);
						trailer1.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						trailer1.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						if (type.equals("I")) {
							result = receiptService.insertReceipt(trailer1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(trailer1.toMGData("INSERT"));
							}
						} else {
							result = receiptService.updateReceipt(trailer1);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(trailer1.toMGData("UPDATE"));
							}
						}
					}
				}
				if (result) {
					ShowMessage(result);
					listTrailer = receiptService.FindALLTrailer();
					trailerAdapter.setSource(listTrailer);
					trailerAdapter.notifyDataSetChanged();
					Keyboard();
					btn_trailer_add.setEnabled(true);
					btn_trailer_delete.setEnabled(true);
					btn_trailer_exit.setEnabled(true);
					btn_trailer_save.setEnabled(false);
					btn_trailer_undo.setEnabled(false);
				}
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_trailer_undo:
				listTrailer = receiptService.FindALLTrailer();
				trailerAdapter.setSource(listTrailer);
				trailerAdapter.ScrollToRow(0);
				
				btn_trailer_add.setEnabled(true);
				btn_trailer_delete.setEnabled(true);
				btn_trailer_exit.setEnabled(true);
				btn_trailer_save.setEnabled(false);
				btn_trailer_undo.setEnabled(false);
				lv_choose.setEnabled(true);
				break;
			//printer
			case R.id.btn_pr_add:
				PrinterSetting pr = new PrinterSetting();
				pr.setPs_id(mPrinterSettingService.getMaxPrintNo() + 1);
				pr.setType("I");
				pr.setNo(listPrinter.size() + 1);
				
				mPrinterEditDialog = new PrinterEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mPrinterEditDialog.setSource(listPrinter, pr, "add");
				mPrinterEditDialog.show();
				break;
			case R.id.btn_pr_delete:
				if (listPrinter.size() == 0)
					return;
//				row = printerAdapter.GetSelectedIndex();
//				printer_no = listPrinter.get(row).getPs_id();
//				if (mPrinterSettingService.ItemHavePrinter(printer_no)) {
//					ShowMessage(
//							"The printer setting cannot be removed because it is linked to items.",
//							mMessageDialog.MESSAGE_BOX_OK,
//							mMessageDialog.ICON_INFO);
//					return;
//				}
				msgType = 6;
				ShowMessage("Do you want to delete this printer?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_pr_edit:
				if(listPrinter.size() == 0) return;
				row = printerAdapter.GetSelectedIndex();
				PrinterSetting mPrinterSetting = listPrinter.get(row);
				
				mPrinterEditDialog = new PrinterEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mPrinterEditDialog.setSource(listPrinter, mPrinterSetting, "edit");
				mPrinterEditDialog.show();
				break;
			case R.id.btn_pr_save:
				for (int i = 0; i < listPrinter.size(); i++) {
					PrinterSetting printer = listPrinter.get(i);
					type = listPrinter.get(i).getType();
					if (listPrinter.get(i).isEdit()) {
						printer.setType("P");
						printer.setPs_name(listPrinter.get(i).getPs_name());
						printer.setPs_ip(listPrinter.get(i).getPs_ip());
						printer.setPs_port(listPrinter.get(i).getPs_port());
						if (type.equals("I")) {
							result = mPrinterSettingService.Insert(printer);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(printer.toMGData("INSERT"));
							}
						} else {
							result = mPrinterSettingService.Update(printer);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(printer.toMGData("UPDATE"));
							}
						}
					}
				}
				if (result) {
					ShowMessage(result);
					listPrinter = mPrinterSettingService.FindAll();
					printerAdapter.setSource(listPrinter);
					printerAdapter.notifyDataSetChanged();
					Keyboard();
					btn_pr_add.setEnabled(true);
					btn_pr_delete.setEnabled(true);
					btn_pr_exit.setEnabled(true);
					btn_pr_save.setEnabled(false);
					btn_pr_undo.setEnabled(false);
				}
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_pr_undo:
				listPrinter = mPrinterSettingService.FindAll();
				printerAdapter.setSource(listPrinter);
				printerAdapter.ScrollToRow(0);
				
				btn_pr_add.setEnabled(true);
				btn_pr_delete.setEnabled(true);
				btn_pr_exit.setEnabled(true);
				btn_pr_save.setEnabled(false);
				btn_pr_undo.setEnabled(false);
				lv_choose.setEnabled(true);
				break;
			//Rounding
			case R.id.btn_ro_add:
				RoundingSetting rs = new RoundingSetting();
				rs.setRs_id(mRoundingSettingService.getMaxRoundNo() + 1);
				rs.setType("I");
				rs.setNo(listRound.size() + 1);
				rs.setRs_optional("0");
				
				mRoundEditDialog = new RoundEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mRoundEditDialog.setSource(listRound, rs, "add");
				mRoundEditDialog.show();
				break;
			case R.id.btn_ro_delete:
				if (listRound.size() == 0)
					return;
				msgType = 7;
				ShowMessage("Do you want to delete this rounding?",
						mMessageDialog.MESSAGE_BOX_YES_NO,
						mMessageDialog.ICON_INFO);
				break;
			case R.id.btn_ro_edit:
				if(listRound.size() == 0) return;
				row = roundAdapter.GetSelectedIndex();
				RoundingSetting mRoundingSetting = listRound.get(row);
				
				mRoundEditDialog = new RoundEditDialog(PosSettingActivity.this, PosSettingActivity.this);
				mRoundEditDialog.setSource(listRound, mRoundingSetting, "edit");
				mRoundEditDialog.show();
				break;
			case R.id.btn_ro_save:
				for (int i = 0; i < listRound.size(); i++) {
					RoundingSetting round = listRound.get(i);
					type = listRound.get(i).getType();
					if (listRound.get(i).isEdit()) {
						round.setType("R");
						round.setRs_name(listRound.get(i).getRs_name());
						round.setRs_down_value(listRound.get(i).getRs_down_value());
						round.setRs_up_value(listRound.get(i).getRs_up_value());
						round.setRs_to_value(listRound.get(i).getRs_to_value());
						round.setRs_optional(listRound.get(i).getRs_optional());
						if (type.equals("I")) {
							result = mRoundingSettingService.Insert(round);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(round.toMGData("INSERT"));
							}
						} else {
							result = mRoundingSettingService.Update(round);
							if (GlobalInfo.getInstance().isMGNetwork) {
								mMGCreateFiles.createrFile(round.toMGData("UPDATE"));
							}
						}
					}
				}
				if (result) {
					ShowMessage(result);
					listRound = mRoundingSettingService.FindAll();
					roundAdapter.setSource(listRound);
					roundAdapter.notifyDataSetChanged();
					Keyboard();
					btn_ro_add.setEnabled(true);
					btn_ro_delete.setEnabled(true);
					btn_ro_exit.setEnabled(true);
					btn_ro_save.setEnabled(false);
					btn_ro_undo.setEnabled(false);
				}
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_ro_undo:
				listRound = mRoundingSettingService.FindAll();
				roundAdapter.setSource(listRound);
				roundAdapter.ScrollToRow(0);
				
				btn_ro_add.setEnabled(true);
				btn_ro_delete.setEnabled(true);
				btn_ro_exit.setEnabled(true);
				btn_ro_save.setEnabled(false);
				btn_ro_undo.setEnabled(false);
				lv_choose.setEnabled(true);
				break;
			////////////////////
			case R.id.btn_unit:
				String unit = btn_unit.getText().toString();
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(unitSource, unit, DIALOG_WEIGHT_UNIT);
				mStringSourceDialog.show();
				break;
			case R.id.btn_digits:
				String digit = btn_digits.getText().toString();
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(digitSource, digit, DIALOG_PRICE_DIGHTS);
				mStringSourceDialog.show();
				break;
			case R.id.btn_sign:
				String sign = btn_sign.getText().toString();
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(signSource, sign, DIALOG_CURRENCY_SIGN);
				mStringSourceDialog.show();
				break;
			case R.id.btn_fs_tax_rule:
				String rule = btn_fs_tax_rule.getText().toString();
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(ruleSource, rule, DIALOG_FS_TAX_RULE);
				mStringSourceDialog.show();
				break;
			case R.id.btn_label_printer:
				String label_printer = btn_label_printer.getText().toString();
				listPrinter = mPrinterSettingService.FindAll();
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(AdapterDataSourceBuilder.readValueFromPrinterSetting(listPrinter), label_printer, DIALOG_PRINTER);
				mStringSourceDialog.show();
				break;
			case R.id.btn_receipt_printer:
				String receipt_printer = btn_receipt_printer.getText().toString();
				listPrinter = mPrinterSettingService.FindAll();
				listPrinter.add(0, new PrinterSetting(0, "Default Printer", "", 0));
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(AdapterDataSourceBuilder.readValueFromPrinterSetting(listPrinter), receipt_printer, DIALOG_RECEIPT_PNT);
				mStringSourceDialog.show();
				break;
			case R.id.btn_rounding:
				String rounding = btn_rounding.getText().toString();
				listRound = mRoundingSettingService.FindAll();
				mStringSourceDialog = new StringSourceDialog(PosSettingActivity.this, PosSettingActivity.this);
				mStringSourceDialog.setSource(AdapterDataSourceBuilder.readValueFromRoundingSetting(listRound), rounding, DIALOG_ROUNDING);
				mStringSourceDialog.show();
				break;
			case R.id.btn_cpy_save:

				String ss_id = String.valueOf(et_name.getTag());
				String ss_businessName,
				ss_name,
				ss_other_name,
				ss_address_1,
				ss_address_2,
				ss_city,
				ss_state,
				ss_zip,
				ss_telephone,
				ss_fax,
				ss_website;

				ss_businessName = String.valueOf(et_businessName.getText());
				ss_name = String.valueOf(et_name.getText());
				ss_other_name = String.valueOf(et_desc.getText());
				ss_address_1 = String.valueOf(et_address_1.getText());
				ss_address_2 = String.valueOf(et_address_2.getText());
				ss_city = String.valueOf(et_city.getText());
				ss_state = String.valueOf(btn_state.getText());
				ss_zip = String.valueOf(et_zip.getText());
				ss_telephone = String.valueOf(et_telephone.getText());
				ss_fax = String.valueOf(et_fax.getText());
				ss_website = String.valueOf(et_website.getText());
				String sql = "update t_store_setting set ";
				String sql_middle = "";
				if (ss_name.trim().equals("")) {
					ShowMessage("name cannot be empty.",
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
					;
					return;
				} else {
					sql_middle = "ss_name = '" + ss_name + "'";
				}
				if (ss_other_name.trim().equals("")) {
					sql_middle += ",ss_other_name = " + null;
				} else {
					sql_middle += ",ss_other_name = '" + ss_other_name + "'";
				}
				if (ss_address_1.trim().equals("")) {
					sql_middle += ",ss_address_1 = " + null;
				} else {
					sql_middle += ",ss_address_1 = '" + ss_address_1 + "'";
				}
				if (ss_address_2.trim().equals("")) {
					sql_middle += ",ss_address_2 = " + null;
				} else {
					sql_middle += ",ss_address_2 = '" + ss_address_2 + "'";
				}
				if (ss_city.trim().equals("")) {
					sql_middle += ",ss_city = " + null;
				} else {
					sql_middle += ",ss_city = '" + ss_city + "'";
				}
				if (ss_state.trim().equals("")) {
					sql_middle += ",ss_state = " + null;
				} else {
					sql_middle += ",ss_state = '" + ss_state + "'";
				}
				if (ss_zip.trim().equals("")) {
					sql_middle += ",ss_zip = " + null;
				} else {
					sql_middle += ",ss_zip = '" + ss_zip + "'";
				}
				if (ss_telephone.trim().equals("")) {
					sql_middle += ",ss_telephone = " + null;
				} else {
					sql_middle += ",ss_telephone = '" + ss_telephone + "'";
				}
				if (ss_fax.trim().equals("")) {
					sql_middle += ",ss_fax = " + null;
				} else {
					sql_middle += ",ss_fax = '" + ss_fax + "'";
				}
				if (ss_website.trim().equals("")) {
					sql_middle += ",ss_website = " + null;
				} else {
					sql_middle += ",ss_website = '" + ss_website + "'";
				}
				sql_middle += ",update_datetime = "
						+ "strftime('%Y%m%d%H%M%S','now',";
				
				if (ss_businessName.trim().equals("")) {
					sql_middle += ",ss_business_name = " + null;
				} else {
					sql_middle += ",ss_business_name = '" + ss_businessName + "'";
				}
				sql += sql_middle + " where ss_id = " + ss_id;

				StoreSetting ss = new StoreSetting(Integer.valueOf(ss_id), 
						ss_name.equals("")?null:ss_name,
						ss_other_name.equals("")?null:ss_other_name, 
						ss_address_1.equals("")?null:ss_address_1, 
						ss_address_2.equals("")?null:ss_address_2, 
						ss_city.equals("")?null:ss_city, 
						ss_state.equals("")?null:ss_state, 
						ss_zip.equals("")?null:ss_zip, 
						ss_telephone.equals("")?null:ss_telephone, 
						ss_fax.equals("")?null:ss_fax, 
						ss_website.equals("")?null:ss_website,
						ss_businessName.equals("")?null:ss_businessName);
				ss.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				ss.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				String error = storeService.UpdateStoreSetting(sql);
				if (error.length() > 0) {
					ShowMessage("Save Error." + error,
							mMessageDialog.MESSAGE_BOX_OK,
							mMessageDialog.ICON_INFO);
				} else {
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(ss.toMGData("UPDATE"));
					}
					Toast.makeText(PosSettingActivity.this, "Save Completed.",
							Toast.LENGTH_SHORT).show();
					bindStoreData();
				}
				SetEnabledTrue(7);
				Keyboard();
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_cpy_undo:
				ChangeMode.mode = 0;
				bindStoreData();
				ChangeMode.mode = 7;
				et_name.requestFocus();
				btn_cpy_save.setEnabled(false);
				btn_cpy_undo.setEnabled(false);
				btn_cpy_exit.setEnabled(true);
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_state:
				StateDialogWindow();
				break;
			//inventory
			case R.id.btn_inv_save:
				Keyboard();
//				String on_off,ip;
				if(cb_inv_server_on_off.isChecked()){
					on_off = "1";
				}else{
					on_off = "0";
				}
				ip = et_inv_server_ip.getText().toString().trim();
				if(cb_inv_server_on_off.isChecked()){
					if(ip.equals("") || !MoleQUtil.checkIp(ip)){
						ShowMessage("Invalid inventory server IP address.",
								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
						return;
					}
				}else{
					if(!ip.equals("") && !MoleQUtil.checkIp(ip)){
						ShowMessage("Invalid inventory server IP address.",
								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
						return;
					}
				}
//				mInventorySettingService.Delete();
				InventorySetting temp = new InventorySetting();
				temp.setIs_onoff(on_off);
				temp.setIs_server_ip(ip);
				mInventorySettingService.Update(temp);
//				mInventorySettingService.Insert(temp);
				SetEnabledTrue(8);
				if (GlobalInfo.getInstance().isMGNetwork) {
					mMGCreateFiles.createrFile(temp.toMGData("UPDATE"));// UPDATE,DELETE
				}
				Toast.makeText(PosSettingActivity.this, "Save Completed.",
						Toast.LENGTH_SHORT).show();
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_inv_undo:
				ChangeMode.mode = 0;
				bindInventory();
				ChangeMode.mode = 8;
				et_inv_server_ip.requestFocus();
				btn_inv_save.setEnabled(false);
				btn_inv_undo.setEnabled(false);
				btn_inv_exit.setEnabled(true);
				lv_choose.setEnabled(true);
				break;
			//dvr
			case R.id.btn_dvr_save:
				Keyboard();
				int dvr_port = 0;
				if (cb_dvr_server_on_off.isChecked()) {
					on_off = "1";
				}else {
					on_off = "0";
				}
				ip = et_dvr_ip.getText().toString().trim();
				port = et_dvr_port.getText().toString().trim();
				if (cb_dvr_server_on_off.isChecked()) {
					if (ip.equals("") || !MoleQUtil.checkIp(ip)) {
						ShowMessage("Invalid dvr IP address.",
								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
						et_dvr_ip.requestFocus();
						return;
					}
//					if (port.equals("")) {
//						ShowMessage("Invalid dvr port.",
//								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
//						et_dvr_port.requestFocus();
//						return;
//					}
					if (port.equals("")) {
						dvr_port = 0;
					}else {
						dvr_port = Integer.valueOf(port);
					}
				}else {
					if(!ip.equals("") && !MoleQUtil.checkIp(ip)){
						ShowMessage("Invalid dvr IP address.",
								mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
						return;
					}
					if (port.equals("")) {
						dvr_port = 0;
					}else {
						dvr_port = Integer.valueOf(port);
					}
				}
				DvrSetting dvr = new DvrSetting();
				dvr.setDs_onoff(on_off);
				dvr.setDs_dvr_ip(ip);
				dvr.setDs_port(dvr_port);
				mDvrSettingService.Update(dvr);
				SetEnabledTrue(11);
				if (GlobalInfo.getInstance().isMGNetwork) {
					mMGCreateFiles.createrFile(dvr.toMGData("UPDATE"));// UPDATE,DELETE
				}
				Toast.makeText(PosSettingActivity.this, "Save Completed.",
						Toast.LENGTH_SHORT).show();
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_dvr_undo:
				ChangeMode.mode = 0;
				bindDVRSetting();
				ChangeMode.mode = 11;
				et_dvr_ip.requestFocus();
				btn_dvr_save.setEnabled(false);
				btn_dvr_undo.setEnabled(false);
				btn_dvr_exit.setEnabled(true);
				lv_choose.setEnabled(true);
				break;
			case R.id.btn_mult_coupon_add:
//				asd;
				et_mult_coupon_name.setText("");
				et_mult_coupon_qty.setText("");
				et_mult_coupon_add_value.setText("");
				et_mult_coupon_max_value.setText("");
				et_mult_coupon_max_total.setText("");
				SetEnabledFalse(12);
				et_mult_coupon_name.setEnabled(true);
				et_mult_coupon_qty.setEnabled(true);
				et_mult_coupon_add_value.setEnabled(true);
				et_mult_coupon_max_value.setEnabled(true);
				et_mult_coupon_max_total.setEnabled(true);
				cb_mult_coupon_active.setEnabled(false);
				cb_mult_coupon_active.setChecked(false);
				currentProcessMode = Insert_action;
				btn_mult_coupon_add.requestFocus();
				break;
			case R.id.btn_mult_coupon_undo:
				ChangeMode.mode = 0;
				bindMultCouponSetting();
				GetMultCouponSettingDetail(mMultCouponAdapter.GetSelectedIndex());
				ChangeMode.mode = 12;
				SetEnabledTrue(12);
				lv_choose.setEnabled(true);
				currentProcessMode = Update_action;
				break;
			case R.id.btn_mult_coupon_save:
				MultCouponSetting item = null;
				switch (currentProcessMode) {
				case Insert_action:
					item = new MultCouponSetting();
					break;
				case Update_action:
					item = listMultCouponSetting.get(mMultCouponAdapter.GetSelectedIndex());
					break;
				default:
					break;
				}
				
				String name = String.valueOf(et_mult_coupon_name.getText());
				String add_valueString = String.valueOf(et_mult_coupon_add_value.getText());
				String max_valueString = String.valueOf(et_mult_coupon_max_value.getText());
				String max_totalString = String.valueOf(et_mult_coupon_max_total.getText());
				double add_value = 0,max_value = 0,max_total = 0;
				boolean active = cb_mult_coupon_active.isChecked();
				if(name == null || name.trim().equals("")){
					ShowMessage("Name can not be empty.",
							mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
					return;
				}
				if(String.valueOf(et_mult_coupon_qty.getText()).trim().equals("")){
					ShowMessage("Multiply can not be empty.",
							mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
					return;
				}
				if(!(add_valueString == null || add_valueString.trim().equals(""))){
					add_value = Double.valueOf(add_valueString);
				}
				if(!(max_valueString == null || max_valueString.trim().equals(""))){
					max_value = Double.valueOf(max_valueString);
				}
				if(!(max_totalString == null || max_totalString.trim().equals(""))){
					max_total = Double.valueOf(max_totalString);
				}
				int mult = Integer.valueOf(String.valueOf(et_mult_coupon_qty.getText()));
				if(mult <= 1){
					ShowMessage("Multiply can not be less than 1.",
							mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
					return;
				}
				item.setMc_cpn_name(name);
				item.setMc_mult(mult);
				item.setMc_add_value(add_value);
				item.setMc_max_value(max_value);
				item.setMc_max_total(max_total);
				item.setMc_active(active);
				switch (currentProcessMode) {
				case Insert_action:
					mMultCouponSettingService.insert(item);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(item.toMGData("INSERT"));
					}
					break;
				case Update_action:
					mMultCouponSettingService.update(item);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(item.toMGData("UPDATE"));
					}
					break;
				default:
					break;
				}
				
				ChangeMode.mode = 0;
				bindMultCouponSetting();
				GetMultCouponSettingDetail(mMultCouponAdapter.GetSelectedIndex());
				ChangeMode.mode = 12;
				btn_mult_coupon_add.setEnabled(true);
				btn_mult_coupon_exit.setEnabled(true);
				btn_mult_coupon_undo.setEnabled(false);
				btn_mult_coupon_save.setEnabled(false);
				lv_mult_coupon_list.setEnabled(true);
				cb_mult_coupon_active.setEnabled(true);
				lv_choose.setEnabled(true);
				currentProcessMode = Update_action;
				break;
			case R.id.btn_mult_coupon_delete:
				if(listMultCouponSetting.size() > 0){
					msgType = 12;
					ShowMessage("Are you sure to delete this item?",
							mMessageDialog.MESSAGE_BOX_YES_NO,
							mMessageDialog.ICON_INFO);
//					MessageBox.show(PosSettingActivity.this,
//							"Are you sure to delete this item?",
//							onClickMessageListener,
//							MessageDialog.MESSAGE_BOX_YES_NO);
				}
				break;
			case R.id.btn_ta_exit:
			case R.id.btn_bo_exit:
			case R.id.btn_inv_exit:
			case R.id.btn_header_exit:
			case R.id.btn_trailer_exit:
			case R.id.btn_cpy_exit:
			case R.id.btn_pr_exit:
			case R.id.btn_ro_exit:
			case R.id.btn_dvr_exit:
			case R.id.btn_mult_coupon_exit:
				ShowExit();
				break;
			case R.id.al_posSetting:
			case R.id.al_company_info:
			case R.id.al_tax:
			case R.id.al_inventorySetting:
			case R.id.al_dvrSetting:
			case R.id.al_mult_coupon_detailed:
				Keyboard();
				break;
			}
		}
	};

	Dialog stateDialog = null;
	private void StateDialogWindow() {
		bindStateData();
		if (stateDialog == null) {
			stateDialog = new Dialog(this, R.style.Transparent_dialog);
			stateDialog.setContentView(R.layout.dialog_perview_states);
		}
		lv_states_dialog_list = (ListView) stateDialog.findViewById(R.id.lv_states_dialog_list);
		btn_states_dialog_ok = (Button) stateDialog.findViewById(R.id.btn_states_dialog_ok);
		btn_states_dialog_cancel = (Button) stateDialog.findViewById(R.id.btn_states_dialog_cancel);
		btn_states_dialog_search = (Button) stateDialog.findViewById(R.id.btn_states_dialog_search);
		et_states_dialog_search = (EditText) stateDialog.findViewById(R.id.et_states_dialog_search);
		btn_states_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_states_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		btn_states_dialog_search.setOnClickListener(OnClickDialogWindowListener);
		lv_states_dialog_list.setOnItemClickListener(onItemClickDialogWindowListener);
		if (stateAdapter == null) {
			stateAdapter = new StateListAdapter(this, listStates);
			lv_states_dialog_list.setAdapter(stateAdapter);
		} else {
			stateAdapter.setSource(listStates);
			stateAdapter.notifyDataSetChanged();
		}
		String stateString = btn_state.getText().toString().trim();
		if (stateString.equals("")) {
			stateAdapter.ScrollToRow(0);
			lv_states_dialog_list.setSelection(0);
		} else {
			for (int i = 0; i < listStates.size(); i++) {
				if (stateString.equals(listStates.get(i).getState_id())) {
					stateAdapter.ScrollToRow(i);
					lv_states_dialog_list.setSelection(i);
				}
			}
		}
		stateDialog.show();
	}

	public OnItemClickListener onItemClickDialogWindowListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (parent.getId()) {
			case R.id.lv_states_dialog_list:
				stateAdapter.ScrollToRow(position);
				break;
			}

		}
	};
	public OnClickListener OnClickDialogWindowListener = new OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_states_dialog_ok:
				if (listStates.size() > 0) {
					btn_state.setText(listStates.get(
							stateAdapter.GetSelectedIndex()).getState_id());
				}
				stateDialog.dismiss();
				break;
			case R.id.btn_states_dialog_cancel:
				stateDialog.dismiss();
				break;
			case R.id.btn_states_dialog_search:
				String searchStates = et_states_dialog_search.getText()
						.toString();
				listStates = statesService.getStatesById(searchStates);
				stateAdapter.setSource(listStates);
				stateAdapter.notifyDataSetChanged();
				stateAdapter.ScrollToRow(0);
				et_states_dialog_search.setText("");
				break;
			}
		}
	};

	public void SetTaxEnabledTrue() {
		et_rate.setEnabled(true);
		et_startvalue.setEnabled(true);
		et_round.setEnabled(true);
	}

	public void SetTaxEnabledFalse() {
		et_rate.setEnabled(false);
		et_startvalue.setEnabled(false);
		et_round.setEnabled(false);
		cb_byline.setChecked(false);
		cb_vat.setChecked(false);
	}

	public int taxValitation() {
		String rate, start, round;
		double rate1;

		rate = et_rate.getText().toString();
		start = et_startvalue.getText().toString();
		round = et_round.getText().toString();

		if (rate.equals("")) {
			ShowMessage("Rate can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_rate.requestFocus();
			return -1;
		}
		rate1 = Double.valueOf(rate);
		if (rate1 > 100) {
			ShowMessage("Rate can not be more than 100%",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_rate.requestFocus();
			return -1;
		}
		if (start.equals("")) {
			ShowMessage("Start Value can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_startvalue.requestFocus();
			return -1;
		}
		if (round.equals("")) {
			ShowMessage("+ To Round can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_round.requestFocus();
			return -1;
		}
		return 1;
	}

	public int validation() {
		String store, station, open_time, close_time, tare_rate, fc_mix, sig_min;

		store = et_store.getText().toString();
		station = et_station.getText().toString();
		open_time = et_openTime.getText().toString();
		close_time = et_closeTime.getText().toString();
		tare_rate = et_tareRate.getText().toString();
		fc_mix = et_fcMix.getText().toString();
		sig_min = et_sig_min.getText().toString();

		if (store.equals("")) {
			ShowMessage("Store can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_store.requestFocus();
			return -1;
		}
		if (station.equals("")) {
			ShowMessage("Station can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_station.requestFocus();
			return -1;
		}
		if (open_time.equals("")) {
			ShowMessage("OpenTime can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return -1;
		}
		if (close_time.equals("")) {
			ShowMessage("CloseTime can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return -1;
		}
		int open = Integer.valueOf(gettime_short(open_time));
		int close = Integer.valueOf(gettime_short(close_time));
		if (open == close) {
			ShowMessage("CloseTime and OpenTime can not be the same.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			return -1;
		}
		if (tare_rate.equals(".")) {
			ShowMessage("Please enter a valid Tare Rate.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_tareRate.requestFocus();
			return -1;
		}
		if (tare_rate.equals("") || Double.valueOf(tare_rate) >= 100) {
			ShowMessage("Please enter a valid Tare Rate.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_tareRate.requestFocus();
			return -1;
		}
		if (fc_mix.equals("")) {
			ShowMessage("FlipChart Mix can not be empty.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_fcMix.requestFocus();
			return -1;
		}
		if (sig_min.equals(".")) {
			ShowMessage("Please enter a valid Sig Min.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_tareRate.requestFocus();
			return -1;
		}
		if (sig_min.equals("")) {
			ShowMessage("Please enter a valid Sig Min.",
					mMessageDialog.MESSAGE_BOX_OK, mMessageDialog.ICON_INFO);
			et_tareRate.requestFocus();
			return -1;
		}
		return 1;
	}

	public OnClickListener onTextClickListener = new OnClickListener() {

		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.et_openTime:
				if (GlobalInfo.getInstance().calendar_type) {
					tdssDialog.show(R.id.et_openTime,et_openTime.getText().toString());
				}else{
					itidDialog.show(R.id.et_openTime,et_openTime.getText().toString());
				}
				break;
			case R.id.et_closeTime:
				if (GlobalInfo.getInstance().calendar_type) {
					tdssDialog.show(R.id.et_closeTime,et_closeTime.getText().toString());
				}else{
					itidDialog.show(R.id.et_closeTime,et_closeTime.getText().toString());
				}
				break;
			default:
				break;
			}
		}
	};

	public String gettime(String time) {

		if (time == null || time.equals("")) {
			return "";
		} else {
			String str = time.substring(0, 2);
			String str2 = time.substring(2, 4);
			return str + ":" + str2;
		}
	};

	public String gettime_short(String time) {
		String str;
		if (time == null) {
			str = "";
		} else {
			time = time + "00";
			str = time.replace(":", "");
		}
		return str;
	}

	public void UpdateMessage(int msg) {
		switch (msg) {
		case 1:
			Toast.makeText(PosSettingActivity.this, "Save Completed.",
					Toast.LENGTH_SHORT).show();
			break;
		case 0:
			break;
		case -1:
			ShowMessage("Save Error.", mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_INFO);
			break;
		}
	}
	
	public void ShowMessage(boolean re) {
		if (re) {
			Toast.makeText(PosSettingActivity.this, "Save Completed.",
					Toast.LENGTH_SHORT).show();
		}else {
			ShowMessage("Save Error.", mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_INFO);
		}
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		int index, hid, tid, tare_no, bottle_no, printer_id,round_id;
		String type;

		public void onClickYes() {
			
			switch (msgType) {
			case 0:
				finish();
				break;
			case 1: // header
				if (is_copy) {
					Receipt h = null;
					//cannot copy the company name to header
					String storeName = listStore.get(0).getSs_name();
					if (storeName != null) {
						h = new Receipt();
						h.setType("H");
						h.setMsg(storeName);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
//					//copy businessName
//					String businessName = listStore.get(0).getSs_business_name();
//					if (businessName != null) {
//						h = new Receipt();
//						h.setType("H");
//						h.setMsg(businessName);
//						h.setLarge("N");
//						h.setJustify("C");
//						h.setId(receiptService.getMaxReceiptNo());
//						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
//						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
//						receiptService.insertReceipt(h);
//						if (GlobalInfo.getInstance().isMGNetwork) {
//							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
//						}
//					}
					String storeDesc = listStore.get(0).getSs_other_name();
					if (storeDesc != null && !storeDesc.equals("")) {
						h = new Receipt();
						h.setType("H");
						h.setMsg(storeDesc);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					String storeAddress1 = listStore.get(0).getSs_address_1();
					if (storeAddress1 != null) {
						h = new Receipt();
						h.setType("H");
						h.setMsg(storeAddress1);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					String storeAddress2 = listStore.get(0).getSs_address_2();
					if (storeAddress2 != null) {
						h = new Receipt();
						h.setType("H");
						h.setMsg(storeAddress2);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					String storeCity = listStore.get(0).getSs_city();
					String storeState = listStore.get(0).getSs_state();
					String storeZip = listStore.get(0).getSs_zip();

					if (storeCity != null || storeState != null
							|| storeZip != null) {
						if (storeCity == null)
							storeCity = "";
						if (storeState == null)
							storeState = "";
						if (storeZip == null)
							storeZip = "";
						String Temp = "";
						if (storeCity.equals("")) {
							Temp = storeState + " " + storeZip;
						} else {
							Temp = storeCity + ", " + storeState + " "
									+ storeZip;
						}
						h = new Receipt();
						h.setType("H");
						h.setMsg(Temp);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					String storeTelephone = listStore.get(0).getSs_telephone();
					if (storeTelephone != null) {
//						if (storeTelephone.length() == 10) {
//							storeTelephone = "("
//									+ storeTelephone.substring(0, 3) + ")"
//									+ storeTelephone.substring(3, 6) + "-"
//									+ storeTelephone.substring(6, 10);
//						}
						h = new Receipt();
						h.setType("H");
						h.setMsg("Tel: " + storeTelephone);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					String storeFax = listStore.get(0).getSs_fax();
					if (storeFax != null) {
						h = new Receipt();
						h.setType("H");
						h.setMsg("Fax:" + storeFax);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					String storeWebsite = listStore.get(0).getSs_website();
					if (storeWebsite != null) {
						h = new Receipt();
						h.setType("H");
						h.setMsg(storeWebsite);
						h.setLarge("N");
						h.setJustify("C");
						h.setId(receiptService.getMaxReceiptNo());
						h.setCreateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						h.setUpdateTime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
						receiptService.insertReceipt(h);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(h.toMGData("INSERT"));
						}
					}
					listHeader = receiptService.FindALLHeader();
					headerAdapter.setSource(listHeader);
					headerAdapter.notifyDataSetChanged();
					headerAdapter.ScrollToRow(listHeader.size() - 1);
					lv_header.setSelection(lv_header.getAdapter().getCount() - 1);
					is_copy = false;
					lv_choose.setEnabled(true);
					mMessageDialog.dismiss();
				}else{
					index = headerAdapter.GetSelectedIndex();
					hid = listHeader.get(index).getId();
					if (hid > 0) {
						result = receiptService.deleteReceipt(hid);
						if (GlobalInfo.getInstance().isMGNetwork) {
							mMGCreateFiles.createrFile(listHeader.get(index).toMGData("DELETE"));
						}
					} else {
						listHeader.remove(index);
						result = true;
					}
					if (result) {
						listHeader = receiptService.FindALLHeader();
						headerAdapter.setSource(listHeader);
						headerAdapter.notifyDataSetChanged();
						mMessageDialog.dismiss();
						if (index != 0) {
							headerAdapter.ScrollToRow(index - 1);
						} else {
							headerAdapter.ScrollToRow(0);
						}
					}
					lv_choose.setEnabled(true);
				}
				break;
			case 2: // trailer
				index = trailerAdapter.GetSelectedIndex();
				tid = listTrailer.get(index).getId();
				if (tid > 0) {
					result = receiptService.deleteReceipt(tid);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(listTrailer.get(index).toMGData("DELETE"));
					}
				} else {
					listTrailer.remove(index);
					result = true;
				}
				if (result) {
					listTrailer = receiptService.FindALLTrailer();
					trailerAdapter.setSource(listTrailer);
					trailerAdapter.notifyDataSetChanged();
					mMessageDialog.dismiss();
					if (index != 0) {
						trailerAdapter.ScrollToRow(index - 1);
					} else {
						trailerAdapter.ScrollToRow(0);
					}
				}
				lv_choose.setEnabled(true);
				break;
			case 3: // tare
				index = tareAdapter.GetSelectedIndex();
				tare_no = listTare.get(index).getTa_no();
				type = listTare.get(index).getType();
				if (type.equals("I")) {
					listTare.remove(index);
					result = true;
				} else {
					result = tareService.deleteTareSetting(tare_no);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(listTare.get(index).toMGData("DELETE"));
					}
				}
				if (result) {
					listTare = tareService.FindAll();
					tareAdapter.setSource(listTare);
					tareAdapter.notifyDataSetChanged();
					mMessageDialog.dismiss();
					if (index != 0) {
						tareAdapter.ScrollToRow(index - 1);
					} else {
						tareAdapter.ScrollToRow(0);
					}
				}
				lv_choose.setEnabled(true);
				break;
			case 4: // bottle
				index = bottleAdapter.GetSelectedIndex();
				bottle_no = listBottle.get(index).getBo_no();
				type = listBottle.get(index).getType();
				if (type.equals("I")) {
					listBottle.remove(index);
					result = true;
				} else {
					result = bottleService.deleteBottleSetting(bottle_no);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(listBottle.get(index).toMGData("DELETE"));
					}
				}
				if (result) {
					listBottle = bottleService.FindAll();
					bottleAdapter.setSource(listBottle);
					bottleAdapter.notifyDataSetChanged();
					mMessageDialog.dismiss();
					if (index != 0) {
						bottleAdapter.ScrollToRow(index - 1);
					} else {
						bottleAdapter.ScrollToRow(0);
					}
				}
				lv_choose.setEnabled(true);
				break;
			case 5:
				result = psService.updatePosSetting(posSettingx);
				if (result && GlobalInfo.getInstance().isMGNetwork) {
					mMGCreateFiles.createrFile(posSettingx.toMGData("UPDATE"));
				}
				ShowMessage(result);
				bindPosData();
				ChangeMode.mode = 1;
				SetEnabledTrue(1);
				Keyboard();
				lv_choose.setEnabled(true);
				break;
			case 6:
				index = printerAdapter.GetSelectedIndex();
				printer_id = listPrinter.get(index).getPs_id();
				type = listPrinter.get(index).getType();
				if (type.equals("I")) {
					listPrinter.remove(index);
					result = true;
				} else {
					result = mPrinterSettingService.Delete(printer_id);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(listPrinter.get(index).toMGData("DELETE"));
					}
				}
				if (result) {
					listPrinter = mPrinterSettingService.FindAll();
					printerAdapter.setSource(listPrinter);
					printerAdapter.notifyDataSetChanged();
					mMessageDialog.dismiss();
					if (index != 0) {
						printerAdapter.ScrollToRow(index - 1);
					} else {
						printerAdapter.ScrollToRow(0);
					}
				}
				lv_choose.setEnabled(true);
				break;
			case 7:
				index = roundAdapter.GetSelectedIndex();
				round_id = listRound.get(index).getRs_id();
				type = listRound.get(index).getType();
				if (type.equals("I")) {
					listRound.remove(index);
					result = true;
				} else {
					result = mRoundingSettingService.Delete(round_id);
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(listRound.get(index).toMGData("DELETE"));
					}
				}
				if (result) {
					listRound = mRoundingSettingService.FindAll();
					roundAdapter.setSource(listRound);
					roundAdapter.notifyDataSetChanged();
					mMessageDialog.dismiss();
					if (index != 0) {
						roundAdapter.ScrollToRow(index - 1);
					} else {
						roundAdapter.ScrollToRow(0);
					}
				}
				lv_choose.setEnabled(true);
				break;
			case 12:
				if(listMultCouponSetting.size() > 0){
					MultCouponSetting mMultCoupon = listMultCouponSetting.get(mMultCouponAdapter.GetSelectedIndex());
					mMultCouponSettingService.delete(mMultCoupon.getMc_id());
					if (GlobalInfo.getInstance().isMGNetwork) {
						mMGCreateFiles.createrFile(mMultCoupon.toMGData("DELETE"));
					}
					ChangeMode.mode = 0;
					bindMultCouponSetting();
//					int index = mMultCouponAdapter.GetSelectedIndex();
//					int index2 = -1;
//					if (index == 0 && listMultCouponSetting.size() > 1) {
//						index2 = index;
//					} else if (index > 0) {
//						index2 = index - 1;
//					}
					GetMultCouponSettingDetail(0);
					ChangeMode.mode = 12;
				}
				break;
			default:
				break;
			}
		}

		public void onClickNo() {
			is_copy = false;
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};

	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(PosSettingActivity.this);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	
	public void ShowExit(){
		msgType = 0;
		ShowMessage(
				getResources().getString(R.string.msg_exit_program),
				MessageDialog.MESSAGE_BOX_YES_NO,
				MessageDialog.ICON_INFO);
	}

	public void Keyboard() {
		try {
			InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethodManager.hideSoftInputFromWindow(PosSettingActivity.this
					.getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		} catch (Exception e) {
			return;
		}

	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass, int viewId, String parm) {
		super.onCloseWithReturn(pramClass, viewId, parm);

		switch (viewId) {
		case R.id.et_openTime:
			et_openTime.setText(parm);
			break;
		case R.id.et_closeTime:
			et_closeTime.setText(parm);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onReturnValue(int what, Object obj) {
		// TODO Auto-generated method stub
		switch (what) {
		case R.id.btn_tare_ok:
			listTare = (List<TareSetting>) obj;
			tareAdapter.notifyDataSetChanged();
			mEditableListener.onEdited(TareSetupAdapter.TARE_EDITED);
			break;
		case R.id.btn_bottle_ok:
			listBottle = (List<BottleSetting>) obj;
			bottleAdapter.notifyDataSetChanged();
			mEditableListener.onEdited(BottleSetupAdapter.BOTTLE_EDITED);
			break;
		case HEADER_RETURN:
			listHeader = (List<Receipt>) obj;
			mEditableListener.onEdited(ReceiptSetupAdapter.HEADER_EDITED);
			headerAdapter.setSource(listHeader);
			headerAdapter.notifyDataSetChanged();
			break;
		case TRAILER_RETURN:
			listTrailer = (List<Receipt>) obj;
			mEditableListener.onEdited(ReceiptSetupAdapter.TRAILER_EDITED);
			trailerAdapter.setSource(listTrailer);
			trailerAdapter.notifyDataSetChanged();
			break;
		case R.id.btn_printer_ok:
			listPrinter = (List<PrinterSetting>) obj;
			printerAdapter.notifyDataSetChanged();
			mEditableListener.onEdited(PrinterSetupAdapter.PRINT_EDITED);
			break;
		case R.id.btn_round_ok:
			listRound = (List<RoundingSetting>) obj;
			roundAdapter.notifyDataSetChanged();
			mEditableListener.onEdited(RoundSetupAdapter.ROUND_EDITED);
			break;
		case DIALOG_PRINTER:
			String label_printer = (String) obj;
			btn_label_printer.setText(label_printer);
			PrinterSetting printer = mPrinterSettingService.FindByName(label_printer);
			btn_label_printer.setTag(printer);
			break;
		case DIALOG_RECEIPT_PNT:
			String receipt_pnt = (String) obj;
			btn_receipt_printer.setText(receipt_pnt);
			if (receipt_pnt.equals("Default Printer")) {
				btn_receipt_printer.setTag(new PrinterSetting(0, "Default Printer", "", 0));
			}else {
				PrinterSetting receiptPrinter = mPrinterSettingService.FindByName(receipt_pnt);
				btn_receipt_printer.setTag(receiptPrinter);
			}
			break;
		case DIALOG_ROUNDING:
			String rounding = (String) obj;
			btn_rounding.setText(rounding);
			RoundingSetting round = mRoundingSettingService.FindByName(rounding);
			btn_rounding.setTag(round);
			break;
		case DIALOG_FS_TAX_RULE:
			String rule = (String) obj;
			btn_fs_tax_rule.setText(rule);
			break;
		case DIALOG_CURRENCY_SIGN:
			String sign = (String) obj;
			if (sign.equals("-N/A-")) {
				sign = "";
			}else if (sign.equals("$ - US Dollar")) {
				sign = "$";
			}else if (sign.equals("€ - Euro")) {
				sign = "€";
			}
			btn_sign.setText(sign);
			break;
		case DIALOG_WEIGHT_UNIT:
			String unit = (String) obj;
			btn_unit.setText(unit);
			break;
		case DIALOG_PRICE_DIGHTS:
			String digits = (String) obj;
			btn_digits.setText(digits);
			break;
		}
	}
	
	public void GetMultCouponSettingDetail(int position){
		if(listMultCouponSetting.size() <= 0){ return; }
		MultCouponSetting item = listMultCouponSetting.get(position);
		mMultCouponAdapter.ScrollToRow(position);
		et_mult_coupon_name.setText(item.getMc_cpn_name());
		et_mult_coupon_qty.setText(String.valueOf(item.getMc_mult()));
		if(item.getMc_add_value() <= 0){
			et_mult_coupon_add_value.setText("");
		}else{
			et_mult_coupon_add_value.setText(MoleQUtil.fixNumber(item.getMc_add_value()));
		}
		if(item.getMc_max_value() <= 0){
			et_mult_coupon_max_value.setText("");
		}else{
			et_mult_coupon_max_value.setText(MoleQUtil.fixNumber(item.getMc_max_value()));
		}
		if(item.getMc_max_total() <= 0){
			et_mult_coupon_max_total.setText("");
		}else{
			et_mult_coupon_max_total.setText(MoleQUtil.fixNumber(item.getMc_max_total()));
		}
		cb_mult_coupon_active.setChecked(item.getMc_activeCheckBox());
	}
}